Standard Library
struct Match
A successful match: where it is, what it matched, and its capture groups.
since 0.1.0-alpha.1linuxwindowswasm
Overview
A Match is a plain value that holds its own copy of the text, so it stays valid after the string it came from is gone. Absence of a match is None (Regex.find returns Match?); there is no "failed match" value. Offsets and lengths count bytes, not characters.
Examples
Reading a match
Match? m = regex::find("id=42;name=bob", r"(?<key>[a-z]+)=(?<val>\w+)");
if (m != None) {
console.writeln("${m.index} ${m.length} ${m.value}");
console.writeln(m.groups.length());
console.writeln(m.group(1).value);
console.writeln(m.group("val")?.value ?? "");
console.writeln(m.group("nope") == None);
}
0 5 id=42
3
id
42
true
Fields
groups
The capture groups. groups[0] is the whole match, then groups[1] and on are the groups in the order their opening parentheses appear in the pattern.
index
int indexByte offset where the whole match starts in the searched string.
length
int lengthLength of the whole match in bytes.
value
string valueThe text of the whole match.
Methods
group
group(int i) -> GroupGet a capture group by number.
Parameters
- i
- The group number:
0is the whole match,1is the first group.
Returns
The group.
Throws
RuntimeException- when
iis negative or larger than the number of groups.
Examples
Groups by number and by name
Match? m = regex::find("key=value", r"(?<k>\w+)=(?<v>\w+)");
if (m != None) {
console.writeln(m.group(0).value);
console.writeln(m.group(1).value);
console.writeln(m.group("v")?.value ?? "none");
console.writeln(m.group("missing")?.value ?? "none");
}
key=value
key
value
none
group(string name) -> Group | NoneGet a capture group by name.
Parameters
- name
- A name declared in the pattern with
(?<name>...).
Returns
The group, or None when the pattern declares no group of that name.
groupAt
groupAt(int i) -> GroupGet a capture group by number; the same as group(i).
Parameters
- i
- The group number:
0is the whole match,1is the first group.
Returns
The group.
Throws
RuntimeException- when
iis negative or larger than the number of groups.
Examples
Reading a group by number
Match? m = regex::find("2026-10-02", r"(\d+)-(\d+)-(\d+)");
if (m != None) {
console.writeln(m.groupAt(0).value);
console.writeln(m.groupAt(2).value);
}
2026-10-02
10
See also: group