Negation in regular expressions

Steve Holden steve at holdenweb.com
Fri Sep 8 04:12:50 EDT 2006


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 ? Here's an example to illustrate a
> use case:
> 
> 
>>>>import re
> 
> # split with '@' as delimiter
> 
>>>>[g.group() for g in re.finditer('[^@]+', 'This @ is a @ test ')]
> 
> ['This ', ' is a ', ' test ']
> 
> Is it possible to use finditer to split the string if the delimiter was
> more than one char long (say 'XYZ') ? [yes, I'm aware of re.split, but
> that's not the point; this is just an example. Besides re.split returns
> a list, not an iterator]
> 
I think you are looking for "negative lookahead assertions". See the docs.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list