Thread overview | |||||
---|---|---|---|---|---|
|
September 21, 2010 Does the regex module support named captured groups? | ||||
---|---|---|---|---|
| ||||
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 you can get a hash with "year" as key and 1970 as value. Can this be done with D regex module? Trying it throws a RegExpException with the message "*+? not allowed in atom" |
September 21, 2010 Re: Does the regex module support named captured groups? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juanjo Alvarez | 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
|
September 21, 2010 Re: Does the regex module support named captured groups? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tue, 21 Sep 2010 16:22:22 -0400, bearophile <bearophileHUGS@lycos.com> wrote:
> 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 :-)
I guessed that was the case when I looked at the code, but had to ask anyway, I can live with indexed captures anyway.
|
Copyright © 1999-2021 by the D Language Foundation