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

Adam Lindberg <>
Thu Oct 30 08:34:49 CET 2008

When using regular expressions as a part of an API returning named groups is
very desirable. As it is now one has to parse the regular expression (with
another regular expression?) to find the group names and the match them
together with the result. Not a very efficient way of doing things, and
rather error prone.

If the re module knows about the groups, as you can retrieve them by name,
why not just return their names too?

I'd like this feature to be included since it is very powerful together with
groups. Thanks!


2008/10/9 Bjorn Gustavsson <>

> 2008/10/7 Tony Arcieri <>
>> 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
> _______________________________________________
> erlang-questions mailing list
> http://www.erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081030/127c5216/attachment.html>

More information about the erlang-questions mailing list