Skip to content

Commit

Permalink
Switch to matchFirst/matchAll for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-anderson committed Jan 14, 2014
1 parent 18c9764 commit 9dcc377
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions std/regex.d
Expand Up @@ -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);
}
}
Expand All @@ -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}+$`));
---
Expand Down Expand Up @@ -5648,8 +5648,7 @@ public:
import std.regex;
import std.range;
auto m = match("a = 42;", regex(`(?P<var>\w+)\s*=\s*(?P<value>\d+);`));
auto c = m.captures;
auto c = matchFirst("a = 42;", regex(`(?P<var>\w+)\s*=\s*(?P<value>\d+);`));
assert(c["var"] == "a");
assert(c["value"] == "42");
popFrontN(c, 2);
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 9dcc377

Please sign in to comment.