std.regex named matches

Dmitry Olshansky dmitry.olsh at gmail.com
Tue Feb 21 07:45:55 PST 2012


On 21.02.2012 7:34, James Miller wrote:
> On 20 February 2012 21:34, Dmitry Olshansky<dmitry.olsh at gmail.com>  wrote:
>> 08.02.2012 13:07, James Miller пишет:
>>>
>>> Hi,
>>>
>>> I am using std.regex and using the named matches. I would like to be
>>> able to get at the names that have matched, since this is library
>>> code.
>>>
>>> e.g.
>>>
>>>      auto m = match("test/2", regex(r"(?P<word>\w+)/(?P<num>\d)"));
>>>      //either
>>>      auto names = m.names;
>>>      //or
>>>      auto names = m.captures.names;
>>>
>>> or something similar. I've looked at the library and I can't find
>>> anything of the sort, and you can't even use `foreach` to get at them
>>> that way, I'm guessing because you can have both integer and string
>>> indexes for the matches.
>>>
>>
>> I know this is two weeks old, but you can do foreach on them:
>> foreach(c; m.captures){
>>         //c is each captured group in turn, the first one is the whole match
>> }
>>
>>> Thanks
>>>
>>> James Miller
>>
>>
>
> Yeah, the problem with that is that I want the /names/ of the matches,
> that only returns the match. This was for library code, so the
> developer passes the regex. There are workarounds, but I would have
> liked to be able to do something more like
>
>      auto names = m.captures.names;
>      foreach (name; names) {
>         writeln(name,": ", m.captures[name]);
>      }
>
> or even a straight AA-style foreach like this:
>
>      foreach (name, match; m.captures) {
>         writeln(name,": ", match);
>      }
>

Names work as aliases for numbers, so that not every captured group has 
name. But something along the lines of :
foreach(num, match; m.captures)
	writeln(m.nameOf(num),": ",match);

where nameOf(x) should return mm.. empty string for groups with no name?

> it was decided that more data was needed in regex anyway, but there
> was no consensus as to how that should be implemented.

Yes, more thought work needed. And being the guy behind current 
implementation, I'm curious what's your use case and how to best fit it 
in general API.

-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list