Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88666
| Date | 2015-04-08 15:48 +0200 |
|---|---|
| Subject | Get the numbering of named regex groups |
| From | Mattias Ugelvik <uglemat@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.139.1428500897.12925.python-list@python.org> (permalink) |
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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Get the numbering of named regex groups Mattias Ugelvik <uglemat@gmail.com> - 2015-04-08 15:48 +0200
csiph-web