View source code
Display the source code in std/regex.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla 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 usinglocal 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
Name | Type | Description |
---|---|---|
back [get]
|
R | Range interface. |
captures [get]
|
auto | A hook for compatibility with original std.regex. |
empty [get]
|
bool | Range interface. |
front [get]
|
R | Range interface. |
hit [get]
|
R | Slice of matched portion of input. |
length [get]
|
size_t | Number of matches in this object. |
post [get]
|
R | Slice of input immediately after the match. |
pre [get]
|
R | Slice of input prior to the match. |
whichPattern [get]
|
int | Number of pattern matched counting, where 1 - the first pattern. Returns 0 on no match. |
Methods
Name | Description |
---|---|
opCast
()
|
Explicit cast to bool. Useful as a shorthand for !(x.empty) in if and assert statements. |
opIndex
(i)
|
Range interface. |
opIndex
(i)
|
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"));
// Captures that are not matched will be null.
c = matchFirst("ac", regex(`a(b)?c`));
assert(c);
assert(!c[1]);
Authors
Dmitry Olshansky,
API and utility constructs are modeled after the original std
by Walter Bright and Andrei Alexandrescu.
License
Copyright © 1999-2025 by the D Language Foundation | Page generated by ddox.