Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88666 > unrolled thread

Get the numbering of named regex groups

Started byMattias Ugelvik <uglemat@gmail.com>
First post2015-04-08 15:48 +0200
Last post2015-04-08 15:48 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Get the numbering of named regex groups Mattias Ugelvik <uglemat@gmail.com> - 2015-04-08 15:48 +0200

#88666 — Get the numbering of named regex groups

FromMattias Ugelvik <uglemat@gmail.com>
Date2015-04-08 15:48 +0200
SubjectGet the numbering of named regex groups
Message-ID<mailman.139.1428500897.12925.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web