Must be a bug in the re module [was: Why this result with the re module]

John Bond lists at asd-group.com
Wed Nov 3 05:43:18 CET 2010


On 3/11/2010 4:23 AM, Yingjie Lan wrote:
> --- On Wed, 11/3/10, MRAB<python at mrabarnett.plus.com>  wrote:
>
>> From: MRAB<python at mrabarnett.plus.com>
>> Subject: Re: Must be a bug in the re module [was: Why this result with the re module]
>> To: python-list at python.org
>> Date: Wednesday, November 3, 2010, 8:02 AM
>> On 03/11/2010 03:42, Yingjie Lan
>> wrote:
>> Therefore the outer (first) group is always an empty string
>> and the
>> inner (second) group is the same as the previous example
>> (the last
>> capture or '' if no capture).
> Now I am confused also:
>
> If the outer group \1 is empty, how could the inner
> group \2 actually have something?
>
> Yingjie
>
>
>
I just explained that (I think!)! The outer capturing group uses 
repetition, so it returns the last thing that was matched by the inner 
group, which was an empty string. I

If you took away the outer groups repetition:

re.findall('((.a.)*)', 'Mary has a lamb')

then, for each of the six matches, it returns the full thing that was 
matched:

('Mar', 'Mar'), ('', ''), ('', ''), ('has a lam', 'lam'), ('', ''), ('', 
'')]

Cheers, JB



More information about the Python-list mailing list