LEVIATHAN v962456e · 962456eee1

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

Array<Group> 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 index

Byte offset where the whole match starts in the searched string.

length

int length

Length of the whole match in bytes.

value

string value

The text of the whole match.

Methods

group

group(int i) -> Group

Get a capture group by number.

Parameters

i
The group number: 0 is the whole match, 1 is the first group.

Returns

The group.

Throws

RuntimeException
when i is 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 | None

Get 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.

See also: groupAt, group

groupAt

groupAt(int i) -> Group

Get a capture group by number; the same as group(i).

Parameters

i
The group number: 0 is the whole match, 1 is the first group.

Returns

The group.

Throws

RuntimeException
when i is 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

See also

  • find — Find the first match in a string.
  • Group — One capture group of a match: whether it took part, and where and what it matched.