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, [EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to