Is there a way to get the PATTERN for a regex group in a python compiled regex?

Viewed 18

I am writing a helper to troubleshoot more complex regex patterns, and I was wondering if there was a way of getting the pattern for a numbered or named group in a compiled regex?

for example, I would like to do something like

>>> test = re.compile(r'(foo(?P<named1>*.))(the\send)')
>>> test._subgroup(0)
"foo(?P<named1>*.)"
>>> test._subgroup(1)
"?P<named1>*."
>>> test_subgroup('named1')
"*."
>>> test.subgroup(2)
"the\send"

I did look at Get group subpatterns from a compiled regex object, which suggests parsing the pattern string (which I can do), but I was hoping that these are internally stored in the compiled regex object and could be retrieved instead of having another parser.

thoughts?

0 Answers
Related