View source code
Display the source code in std/regex.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Struct std.regex.Captures

Captures object contains submatches captured during a call to match or iteration over RegexMatch range.

struct Captures(R)
  
if (isSomeString!R);

First element of range is the whole match.

Properties

NameTypeDescription
back[get] RRange interface.
captures[get] autoA hook for compatibility with original std.regex.
empty[get] boolRange interface.
front[get] RRange interface.
hit[get] RSlice of matched portion of input.
length[get] size_tNumber of matches in this object.
post[get] RSlice of input immediately after the match.
pre[get] RSlice of input prior to the match.
whichPattern[get] intNumber of pattern matched counting, where 1 - the first pattern. Returns 0 on no match.

Methods

NameDescription
opCast Explicit cast to bool. Useful as a shorthand for !(x.empty) in if and assert statements.
opIndex Range interface.
opIndex Lookup named submatch.
popBack Range interface.
popFront Range interface.

Example

import std.range.primitives : popFrontN;

auto c = matchFirst("@abc#", regex(`(\w)(\w)(\w)`));
assert(c.pre == "@"); // Part of input preceding match
assert(c.post == "#"); // Immediately after match
assert(c.hit == c[0] && c.hit == "abc"); // The whole match
writeln(c[2]); // "b"
writeln(c.front); // "abc"
c.popFront();
writeln(c.front); // "a"
writeln(c.back); // "c"
c.popBack();
writeln(c.back); // "b"
popFrontN(c, 2);
assert(c.empty);

assert(!matchFirst("nothing", "something"));

Authors

Dmitry Olshansky,

API and utility constructs are modeled after the original std.regex by Walter Bright and Andrei Alexandrescu.

License

Boost License 1.0.