match groups: optional groups not accessible

Duncan Booth duncan.booth at invalid.invalid
Fri Jun 10 08:08:36 EDT 2005


david.reitter wrote:

> So I would expect None rather than an IndexError, which is (only?)
> supposed to occur "If a string argument is not used as a group name in
> the pattern".
That is exactly what does happen.

> 
> I would expect named groups and numbered groups to be behave the same
> way.
> Where's my mistake?
Using 'maybe' as a group name when the only group in the pattern is called 
'm'.

> 
>>>> 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
> 




More information about the Python-list mailing list