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

BJörn Lindqvist <>
Wed Oct 29 19:18:54 CET 2008


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.

Yes please! There is no support for anonymous groups either which is
an even bigger omission.


-- 
mvh Björn



More information about the erlang-questions mailing list