www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does the regex module support named captured groups?

reply Juanjo Alvarez <fake fakeemail.com> writes:
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"
Sep 21 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Sep 21 2010
parent Juanjo Alvarez <fake fakeemail.com> writes:
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 

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.
Sep 21 2010