Get nesting of regex groups

Mattias Ugelvik uglemat at gmail.com
Wed Apr 8 16:54:57 EDT 2015


Example: re.compile('(?P<outer>(?P<inner>a))')

How can I detect that 'inner' is a nested group of 'outer'? I know
that 'inner' comes later, because I can use the `regex.groupindex`
(thanks to your help earlier:
https://mail.python.org/pipermail/python-list/2015-April/701594.html).

After looking a bit around, I found this:

>>> sre_parse.parse('(?P<outer>(?P<inner>a))')
[('subpattern', (1, [('subpattern', (2, [('literal', 97)]))]))]

This is all I need, but this is an internal module. Though there
doesn't seem to have been changes from py2 to py3. How inadvisable is
it to use this? Would you blame me?



More information about the Python-list mailing list