Why this result with the re module

Yingjie Lan lanyjie at yahoo.com
Tue Nov 2 03:00:52 EDT 2010


> From: John Bond <lists at asd-group.com>
> Subject: Re: Why this result with the re module
> >>>> re.findall('(.a.)*', 'Mary has a lamb')
> > ['Mar', '', '', 'lam', '', '']

> So - see if you can explain the first "problematic" result
> now.

Thanks a lot for explaining to me the second "problematic" result!
But the first one is even more puzzling...mainly because the
pattern matches any empty string. Here are more examples:

>>> re.findall('(.a.)*','')
['']
>>> re.findall('(.a.)*',' ') #one space
['', '']
>>> re.findall('(.a.)*','  ') #two spaces
['', '', '']
>>> len(re.findall('(.a.)*',' '*4)) #four
5
>>> len(re.findall('(.a.)*',' '*8)) #eight
9

I must need more details of the matching algorithm to explain this?

Regards,

Yingjie



      



More information about the Python-list mailing list