help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regarding replacing regexp


From: XeCycle
Subject: Re: Regarding replacing regexp
Date: Fri, 11 May 2012 11:52:40 +0800
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1.50 (gnu/linux)

Barry Margolin <barmar@alum.mit.edu> writes:

[...]

>> > There are a lot of ways to do it.  One could be:
>> >
>> > String \([^ ]+\)
>> >
>> > and replace it with:
>> >
>> > String \1 = "\1"
>> 
>> This won't do very well.
>
> Except that he forgot the semicolon, what's wrong about it?

May match something more, of course; mine has the same problem.

>> 
>> Try:
>> 
>> SUCCESS\|FAILURE => \& = "\&";
>> 
>> I may be wrong about those escapes, try it.
>
> You're assuming those are the only names he has to replace.  The first 
> responder assumed that they were just examples of a common pattern.

Huh?  Don't know what the OP want, though.

> Plus, your version will replace them in other contexts, e.g. the program 
> is likely to contain something like:
>
> if (...) return SUCCESS;
>
> but you wouldn't want to change this with the replacement.

That's a common problem about such replacements.  Similarly one
may want to define a String object somewhere else, Deniz's
solution will fail, too.  Query replace to the rescue.  It's up
to the OP to decide which one will be better, or combine them to
remove more mismatches:

String \(SUCCESS\|FAILURE\)

Of course, those tools are rarely accurate in such things like
code refactoring.

-- 
Carl Lei (XeCycle)
Department of Physics, Shanghai Jiao Tong University
OpenPGP public key: 7795E591
Fingerprint: 1FB6 7F1F D45D F681 C845 27F7 8D71 8EC4 7795 E591

Attachment: pgp3bQvUCsJy5.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]