Extract the Name and Span of Regex Matched Groups
问题 I have a regex that looks like: rgx = '(?P<foo>ABC)(?P<bar>DEF)?(?P<norf>HIJK)' Getting the matched string is no problem m.group(name) . However, I need to extract the name and span of the matched groups (or even just the span by name) and haven't found a way to do this. I would like to do something like: p = re.compile(p, re.IGNORECASE) m = p.match(targetstring) #then do something to set 'all' to the list of match objects for mo in all print mo.name() + '->' + mo.span() So for example the