Non greedy regex

Thomas Nelson thn at mail.utexas.edu
Thu Dec 14 10:00:26 EST 2006


There's an optional count argument that will give what you want.  Try
re.sub('a.*b','','ababc',count=1)


Carsten Haese wrote:
> 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