digitalmars.D.learn - Does the regex module support named captured groups?
- Juanjo Alvarez (7/7) Sep 21 2010 In Python and C# you can define a regex like:
- bearophile (11/15) Sep 21 2010 D2 Phobos is in beta stage still, and I think its regex don't support na...
- Juanjo Alvarez (7/8) Sep 21 2010 for named captured groups, but there are many things to implement and
"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
Juanjo Alvarez:"Blabla_(? <year>\d{4}) _BLA And then if you match that against a string like: Blabla_1970_BLAD2 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
On Tue, 21 Sep 2010 16:22:22 -0400, bearophile <bearophileHUGS lycos.com> wrote:You may add an enhancement request for Phobos in Bugzilla, askingfor 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.
Sep 21 2010