[erlang-questions] Extracting capture group names from regular expressions
Bjorn Gustavsson
bgustavsson@REDACTED
Thu Oct 9 09:59:12 CEST 2008
2008/10/7 Tony Arcieri <tony@REDACTED>
> 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?
>
>
There is currently no such option or functionality. It could be added in a
future release if it's a general need.
/Bjorn
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081009/987e3e22/attachment.htm>
More information about the erlang-questions
mailing list