diff --git a/std/regex.d b/std/regex.d index ee17c9f323f..e9b84da03e9 100644 --- a/std/regex.d +++ b/std/regex.d @@ -15,12 +15,12 @@ { // Print out all possible dd/mm/yy(yy) dates found in user input. // g - global: find all matches. - auto r = regex(r"\b[0-9][0-9]?/[0-9][0-9]?/[0-9][0-9](?:[0-9][0-9])?\b", "g"); + auto r = regex(r"\b[0-9][0-9]?/[0-9][0-9]?/[0-9][0-9](?:[0-9][0-9])?\b"); foreach(line; stdin.byLine) { // Match returns a range that can be iterated // to get all subsequent matches. - foreach(c; match(line, r)) + foreach(c; matchAll(line, r)) writeln(c.hit); } } @@ -30,15 +30,15 @@ auto ctr = ctRegex!(`^.*/([^/]+)/?$`); // It works just like a normal regex: - auto m2 = match("foo/bar", ctr); // First match found here, if any - assert(m2); // Be sure to check if there is a match before examining contents! - assert(m2.captures[1] == "bar"); // Captures is a range of submatches: 0 = full match. + auto c2 = matchFirst("foo/bar", ctr); // First match found here, if any + assert(!c2.empty); // Be sure to check if there is a match before examining contents! + assert(c2[1] == "bar"); // Captures is a range of submatches: 0 = full match. ... - // The result of the match is directly testable with if/assert/while. + // The result of the $(D matchAll) is directly testable with if/assert/while. // e.g. test if a string consists of letters: - assert(match("Letter", `^\p{L}+$`)); + assert(matchFirst("Letter", `^\p{L}+$`)); --- @@ -5648,8 +5648,7 @@ public: import std.regex; import std.range; - auto m = match("a = 42;", regex(`(?P\w+)\s*=\s*(?P\d+);`)); - auto c = m.captures; + auto c = matchFirst("a = 42;", regex(`(?P\w+)\s*=\s*(?P\d+);`)); assert(c["var"] == "a"); assert(c["value"] == "42"); popFrontN(c, 2); @@ -5771,7 +5770,7 @@ public: Functionality for processing subsequent matches of global regexes via range interface: --- import std.regex; - auto m = match("Hello, world!", regex(`\w+`, "g")); + auto m = matchAll("Hello, world!", regex(`\w+`)); assert(m.front.hit == "Hello"); m.popFront(); assert(m.front.hit == "world");