Finding strings with exceptions

Eddie Corns eddie at holyrood.ed.ac.uk
Mon Dec 3 13:14:10 EST 2001


David Brady <daves_spam_dodging_account at yahoo.com> writes:

>Hello,

...

>What I want to do is find all instances of an
>expression EXCEPT those that also match another
>expression.  The case in point for today was this: I
>was called away from my machine in the middle of
>refactoring some code.  I was renaming a class to
>conform to our project's coding standards, and the new
>name requires a prefix.

I had to think about why this was a problem at all - I think you mean that
you've already done some of them hence they will match.

>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?

The most obvious way is to protect what you've already converted by changing
it to something unique, change the remaining ones then convert the protected
ones back.  For example:

sed 's/fooBarThingy/rumplestiltskin/g' file >file
sed 's/BarThingy/fooBarThingy/g' file >file
sed 's/rumplestiltskin/fooBarThingy/g' file >file

[Obviously you don't do 'file >file' directly but I'm assuming you know that
bit.  I'm also assuming you know unix-speak. ]

Eddie



More information about the Python-list mailing list