Finding strings with exceptions

Graham Ashton graz at
Mon Dec 3 15:38:41 EST 2001

In article <87snasgn1w.fsf at>, "Henrik Motakef"
<henrik at> wrote:

> David Brady <daves_spam_dodging_account at> 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")
> <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' # 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.


More information about the Python-list mailing list