Something confusing about non-greedy reg exp match

r rt8396 at gmail.com
Mon Sep 7 05:02:44 CEST 2009


On Sep 6, 9:46 pm, "gburde... at gmail.com" <gburde... 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" ?

heres one way, but it depends greatly on the actual pattern you
seek...

re.search(r'hello \w+ money', 'hello how are you hello funny
money').group()

your regex matches the whole string because it means ("hello" followed
by any number of *anythings* up to "money") you see?


wisdom = get_enlightend(http://jjsenlightenments.blogspot.com/)



More information about the Python-list mailing list