Does the regex module support named captured groups?

bearophile bearophileHUGS at lycos.com
Tue Sep 21 13:22:22 PDT 2010


Juanjo Alvarez:

> In Python and C# you can define a regex like:
> "Blabla_(? <year>\d{4}) _BLA
> And then if you match that against a string like:
> Blabla_1970_BLA


D2 Phobos is in beta stage still, and I think its regex don't support named groups yet. So you need to use numbers to select the group you want:

import std.stdio, std.regex;

void main() {
    auto re = regex(r"Blabla_(\d{4})_BLA");
    string test = "Blabla_1970_BLA";
    writeln(match(test, re).front.captures[1]);
}


You may add an enhancement request for Phobos in Bugzilla, asking for named captured groups, but there are many things to implement and only few people that implement things, so probably you will have to wait a lot of time :-)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list