Get the numbering of named regex groups
Mattias Ugelvik
uglemat at gmail.com
Wed Apr 8 09:48:15 EDT 2015
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?). An algorithm involving searching for
'(' in the regular expression strings requires dealing with
parentheses in square brackets somehow + non-capturing groups in order
not to create rare bugs (the most dangerous of bugs).
If it's possible to get access to a regex AST, then I'm willing to
work with that (if the structure is stable), as it's quite important
that I solve this problem. I might be willing to drop Python2.7
support.
More information about the Python-list
mailing list