Captures
A successful match along with any capture groups.
Captures values are returned by Regex.match and
yielded by Regex.find. Coercing one with str produces
the text of the overall match.
Implements: std.Index[Int | Str, Match | nil]
Fields
end @ IntThe byte offset of the end of the overall match within the haystack.
start @ IntThe byte offset of the start of the overall match within the haystack.
Methods
(index) key -> (Match | nil)
Reads a capture group by integer index or by name.
Index 0 is the overall match, 1 the first capture group, and so on.
Named groups, written (?<name>...), are read by their name. A group that
did not participate in the match, such as an optional one, reads as nil.
Parameters
| Name | Type | Description |
|---|---|---|
key |
(Int | Str) |
Group index or name. |
Errors
IndexError if the pattern has no such group.
Example
let date = Regex r"(\d{4})-(\d{2})-(\d{2})"
let caps = date.match "2024-03-15"
echo $caps # => 2024-03-15 (overall match)
echo $caps[0] # => 2024-03-15 (overall match)
echo $caps[1] # => 2024
echo $caps[2] # => 03
echo $caps[3] # => 15
let named = Regex r"(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})"
let caps = named.match "2024-03-15"
echo $caps["year"] # => 2024
echo $caps["month"] # => 03
echo $caps["day"] # => 15