Get the numbering of named regex groups

MRAB python at mrabarnett.plus.com
Wed Apr 8 12:34:26 EDT 2015


On 2015-04-08 15:28, Peter Otten wrote:
> Mattias Ugelvik wrote:
>
>> Example: re.match('(?P<first>a?)(?P<second>b?)', '')
>>
>> How can I find out that the group 'first' correlates to the positional
>> regex group 1? I need to know this to resolve crucial ambiguities in a
>> string manipulation tool I'm making. Looking at spans, as the example
>> above illustrates, won't do the job.
>>
>> I can't see a way to do this through the documented interface (at
>> least not in the `re` module?).
>
> Compile and match in two separate steps:
>
>>>> import re
>>>> r = re.compile('(?P<first>a?)(?P<second>b?)')
>
> Find the groups' positions:
>
>>>> r.groupindex
> {'second': 2, 'first': 1}
>
>
> Find the matching substrings:
>
>>>> r.match("a").groupdict()
> {'second': '', 'first': 'a'}
>
> https://docs.python.org/2.7/library/re.html#re.RegexObject.groupindex
>
The match object has an attribute 're' that's the compiled regex:

 >>> m = re.match('(?P<first>a?)(?P<second>b?)', '')
 >>> m.re.groupindex
{'first': 1, 'second': 2}




More information about the Python-list mailing list