Negation in regular expressions

Ant antroy at gmail.com
Fri Sep 8 05:39:46 EDT 2006


Steve Holden wrote:
> George Sakkis wrote:
> > It's always striked me as odd that you can express negation of a single
> > character in regexps, but not any more complex expression. Is there a
> > general way around this shortcoming ?

The whole point of regexes is that they define expressions to match
things. [^x] doesn't express the negation of x, it is shorthand for
[a-wy-z...]. But the intent is still to match something. What you seem
to want is a way of saying "Match anything that doesn't match the
string 'XYZ' (for example)" What do you expect to get back from this?
In the string "abcd XYZ hhh XYZ" for example, "XYZ h" doesn't match
"XYZ", nor does the empty string, nor does the entire string.

> I think you are looking for "negative lookahead assertions". See the docs.

Negative lookahead and lookbehind are great for expressing that you
want to match X as long as it isn't followed by Y ( "X(?!Y)" ) but
won't help much in your finditer example.

Is there a particular reason you don't want to use split?




More information about the Python-list mailing list