Something confusing about non-greedy reg exp match

Duncan Booth duncan.booth at invalid.invalid
Mon Sep 7 13:08:10 EDT 2009


"gburdell1 at gmail.com" <gburdell1 at gmail.com> wrote:

> If I do this:
> 
> import re
> a=re.search(r'hello.*?money',  'hello how are you hello funny money')
> 
> I would expect a.group(0) to be "hello funny money", since .*? is a
> non-greedy match. But instead, I get the whole sentence, "hello how
> are you hello funny money".
> 
> Is this expected behavior? How can I specify the correct regexp so
> that I get "hello funny money" ?
> 
> 
Another option nobody has yet suggested:

>>> re.match(r'.*(hello.*?money)',  'hello how are you hello funny money').group(1)
'hello funny money'

The initial .* will effectively make the search start at the end and work backwards
so it finds the last 'hello' that is followed by 'money' instead of the first.



More information about the Python-list mailing list