Get nesting of regex groups

Denis McMahon denismfmcmahon at
Wed Apr 8 23:30:15 CEST 2015

On Wed, 08 Apr 2015 22:54:57 +0200, Mattias Ugelvik wrote:

> 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:

Pardon me for stating the obvious, but as the person defining the re, and 
assuming you haven't generated another sub-pattern somewhere in the same 
re with the same name, how can inner ever not be a nested group of outer?

Even in the contrived example below, it is clear that the list of tuples 
generated by by findall is of the form:

()[0] = 'outer', ()[1] = 'inner'

from the order of matches principle.



import re

patt = re.compile('(?P<outer>a+(?P<inner>b+))')

result = patt.findall('abaabbaaabbbaaaabbbb')

print result


however if all you are doing is using .search or .find for the first 
match of the pattern, then there should be no scope for confusion anyway.

Denis McMahon, denismfmcmahon at

More information about the Python-list mailing list