<div dir="ltr">I'm trying to write an API which accepts a regexp with arbitrary named capture groups as input.<br><br>I'm using the re module, and have had success using named subgroups if the names are known ahead of time. However, I'd like to return a mapping of arbitrary named capture groups from the original regular expression to the matched values.<br>
<br>Consider the following regular expression with named subpatterns:<br><br>"(?<foo>\\w+) (?<bar>\\w+)"<br><br>If this were applied to the string "baz qux" I'd like to return the following:<br>
<br>[{"foo", "baz"}, {"bar", "qux"}]<br><br>However, it looks as if I must provide the names of the capture groups I want returned in the capture tuple I pass to re:run.<br clear="all">
<br>Is there any way to extract the names of the subgroups from a compiled regular expression? Or is there some option I can pass to re:run that I'm missing which will return a mapping of subgroup names to what they matched? <br>
<br>-- <br>Tony Arcieri<br><a href="http://medioh.com" target="_blank">medioh.com</a><br>
</div>