Non greedy regex

Maksim Kasimov maksim.kasimov at gmail.com
Thu Dec 14 10:06:11 EST 2006


 > print re.sub( 'a.*?b', '', 'ababc' )
 >
 > gives: 'c'


you've forgot "^" at the begging of the pattern (there is another 'ab' before 'c')


print re.sub( '^a.*?b', '', 'ababc' )


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


-- 
Maksim Kasimov



More information about the Python-list mailing list