Finding strings with exceptions
Henrik Motakef
henrik at moskau.hmotakef.homeip.net
Mon Dec 3 13:45:31 EST 2001
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. In the reverse case, I
> could put something in the regex that says, "also find
> an optional prefix 'foo'." But what I really want to
> do is find the class everywhere EXCEPT where that
> 'foo' prefix exists. Anyone know a good way to do
> this?
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>
Nice syntax, huh?
hth
Henrik
More information about the Python-list
mailing list