How to find out which named regular expression group corresponds to which positional regex group

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to find out which named regular expression group corresponds to which positional regex group

Mattias Ugelvik
I'm making a string manipulation tool in which I need to know this correlation.

Take this example: re.match('(?P<first>a?)(?P<second>b?)', '')

I need to know that 'first' is group #1, and 'second' is group #2. I
need this to resolve certain ambiguities. I was hopin


Reply | Threaded
Open this post in threaded view
|

How to find out which named regular expression group corresponds to which positional regex group

Serhiy Storchaka-2
On 08.04.15 16:23, Mattias Ugelvik wrote:
> I'm making a string manipulation tool in which I need to know this correlation.
>
> Take this example: re.match('(?P<first>a?)(?P<second>b?)', '')
>
> I need to know that 'first' is group #1, and 'second' is group #2. I
> need this to resolve certain ambiguities. I was hopin

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