match groups: optional groups not accessible

David Reitter david.reitter at gmail.com
Fri Jun 10 21:43:26 CEST 2005


On 10 Jun 2005, at 20:38, Michael Chermside wrote:
> David Reitter writes:
>
>> Why does the following result in an IndexError?
>>
>      [...]
>
>>>>> import re
>>>>> m = re.match('(?P<m>maybe)?yes', "yes")
>>>>> m.group(1)
>>>>> m.group('maybe')
>>>>>
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>> IndexError: no such group
>>
>
> Because the name of the named group in your example is 'm' not  
> 'maybe'. 'maybe'
> is the text to match.

Oh shoot! how couldn't i see this!
thanks very much for pointing it out...

- D



More information about the Python-list mailing list