[erlang-questions] Extracting capture group names from regular expressions

Tony Arcieri tony@REDACTED
Tue Oct 7 01:34:07 CEST 2008


I'm trying to write an API which accepts a regexp with arbitrary named
capture groups as input.

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.

Consider the following regular expression with named subpatterns:

"(?<foo>\\w+) (?<bar>\\w+)"

If this were applied to the string "baz qux" I'd like to return the
following:

[{"foo", "baz"}, {"bar", "qux"}]

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.

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?


-- 
Tony Arcieri
medioh.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081006/9b691b62/attachment.htm>


More information about the erlang-questions mailing list