Non greedy regex

Carsten Haese carsten at uniqsys.com
Thu Dec 14 09:58:23 EST 2006


On Thu, 2006-12-14 at 06:45 -0800, tibetan.houdini at gmail.com wrote:
> Can someone please explain why these expressions both produce the same
> result? Surely this means that non-greedy regex does not work?
> 
> print re.sub( 'a.*b', '', 'ababc' )
> 
> gives: 'c'
> 
> Understandable. But
> 
> print re.sub( 'a.*?b', '', 'ababc' )
> 
> gives: 'c'
> 
> NOT, understandable. Surely the answer should be: 'abc'

You didn't tell re.sub to only substitute the first occurrence of the
pattern. It non-greedily matches two occurrences of 'ab' and replaces
each of them with ''. Try replacing with some non-empty string instead
to observe the difference between the two behaviors.

-Carsten





More information about the Python-list mailing list