Finding strings with exceptions
Graham Ashton
graz at mindless.com
Mon Dec 3 15:38:41 EST 2001
In article <87snasgn1w.fsf at moskau.hmotakef.homeip.net>, "Henrik Motakef"
<henrik at moskau.hmotakef.homeip.net> wrote:
> David Brady <daves_spam_dodging_account at yahoo.com> writes:
>
>> The old class name (changed to protect the innocent) is BarThingy. The
>> new name is fooBarThingy. Because the new class name contains the old
>> one, simple find-and-replace goes wonky.
>
> What you are looking for is called a "negative lookbehind assertion, and
> looks like this:
>
>>>> import re
>>>> re.compile("(?<!foo)BarThingy")
>>>> re.search("fooBarThingie")
>>>> re.search("BarThingie")
> <SRE_Match object at 0x501c0>
Indeed, or you could look for an empty string on a word boundary with \b;
$ perl -p -i.bak -e 's/\bBarThingy/fooBarThingy/g' file.py # untested
Python supports \b too, so I should have posted that really, but if I was
in your shoes I'd type the above (mainly because I've not looked up the
Python equivalent), probably feeding it into find -exec if you're on Unix.
--
Graham
More information about the Python-list
mailing list