Captures

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

First element of range is the whole match.

Members

Functions

opCast
bool opCast()

Explicit cast to bool. Useful as a shorthand for !(x.empty) in if and assert statements.

opIndex
inout(R) opIndex(size_t i)

Range interface.

opIndex
R opIndex(String i)

Lookup named submatch.

popBack
void popBack()
popFront
void popFront()
back
R back [@property getter]

Range interface.

Properties

captures
ref captures [@property getter]

A hook for compatibility with original std.regex.

empty
bool empty [@property getter]

Range interface.

front
R front [@property getter]

Range interface.

hit
R hit [@property getter]

Slice of matched portion of input.

length
size_t length [@property getter]

Number of matches in this object.

post
R post [@property getter]

Slice of input immediately after the match.

pre
R pre [@property getter]

Slice of input prior to the match.

whichPattern
int whichPattern [@property getter]

Number of pattern matched counting, where 1 - the first pattern. Returns 0 on no match.

Examples

1 import std.range.primitives : popFrontN;
2 
3 auto c = matchFirst("@abc#", regex(`(\w)(\w)(\w)`));
4 assert(c.pre == "@"); // Part of input preceding match
5 assert(c.post == "#"); // Immediately after match
6 assert(c.hit == c[0] && c.hit == "abc"); // The whole match
7 assert(c[2] == "b");
8 assert(c.front == "abc");
9 c.popFront();
10 assert(c.front == "a");
11 assert(c.back == "c");
12 c.popBack();
13 assert(c.back == "b");
14 popFrontN(c, 2);
15 assert(c.empty);
16 
17 assert(!matchFirst("nothing", "something"));
18 
19 // Captures that are not matched will be null.
20 c = matchFirst("ac", regex(`a(b)?c`));
21 assert(c);
22 assert(!c[1]);

Meta

Suggestion Box / Bug Report