Get nesting of regex groups

Denis McMahon denismfmcmahon at gmail.com
Wed Apr 8 17:30:15 EDT 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:
> https://mail.python.org/pipermail/python-list/2015-April/701594.html).

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.

--------------------------------

#!/usr/bin/python

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 gmail.com



More information about the Python-list mailing list