Match. start: int- Module
- std.
regex - Receiver
- Match
- Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.regex in language mode 5.21.
The current compiler selects std/regex.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1213 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 55 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1213 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296
Receiver
Match
Parameter constraints
Match.start and Match.end are read-only nonnegative UTF-8 byte offsets in the original input. They follow the inclusive start and exclusive end of byte intervals. Both are scalar boundaries and the end does not exceed the UTF-8 byte length of the input.
Match.text is the matched substring. An empty match has equal start and end and an empty text value. The offsets are not string grapheme indexes. A match may select a combining mark inside a grapheme.
Encoding the original input with encodeUtf8, slicing its bytes from start to end, and calling decodeUtf8 returns Ok containing Match.text. This relationship also holds for non-ASCII input.
Match.groups returns an independent array indexed by capture number. Entry zero is Some containing the entire match. Capturing parentheses are numbered from one in opening-parenthesis order. Named groups participate in the same numbering and noncapturing groups do not.
A nonparticipating capture has value None(). A participating empty capture has value Some containing an empty string. A repeated capture retains the last participating capture in the selected match.
Match.named returns an independent map from capture names to captured strings. A nonparticipating named capture is absent. A participating empty named capture is present with an empty string. Names are inserted in ascending ASCII order.
Reading a Match field evaluates its receiver once and does not modify the Match or input. Mutating a groups array or named map does not affect a later field read. Field access cannot fail for a Match value.
Related rule: Read match boundaries and capture snapshotsResult
The declared result type is int.
Effects
Failures
Result and effects
LinkSelect this field from a value of its named type without call parentheses.
The inclusive zero-based UTF-8 byte offset of the match.
Resolved contracts
LinkRelated entries
LinkRead match boundaries and capture snapshots
LinkRead match boundaries and capture snapshots
Match.start and Match.end are read-only nonnegative UTF-8 byte offsets in the original input. They follow the inclusive start and exclusive end of byte intervals. Both are scalar boundaries and the end does not exceed the UTF-8 byte length of the input.
Match.text is the matched substring. An empty match has equal start and end and an empty text value. The offsets are not string grapheme indexes. A match may select a combining mark inside a grapheme.
Encoding the original input with encodeUtf8, slicing its bytes from start to end, and calling decodeUtf8 returns Ok containing Match.text. This relationship also holds for non-ASCII input.
Match.groups returns an independent array indexed by capture number. Entry zero is Some containing the entire match. Capturing parentheses are numbered from one in opening-parenthesis order. Named groups participate in the same numbering and noncapturing groups do not.
A nonparticipating capture has value None(). A participating empty capture has value Some containing an empty string. A repeated capture retains the last participating capture in the selected match.
Match.named returns an independent map from capture names to captured strings. A nonparticipating named capture is absent. A participating empty named capture is present with an empty string. Names are inserted in ascending ASCII order.
Reading a Match field evaluates its receiver once and does not modify the Match or input. Mutating a groups array or named map does not affect a later field read. Field access cannot fail for a Match value.
Why
Byte offsets identify every exact match even when a pattern selects part of a grapheme. The byte-slice relationship makes those offsets usable without confusing them with text indexing. Optional numbered captures and omitted named entries distinguish absence from a captured empty string.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 453 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 465 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 478 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 488 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 518 -
topaz.stdlib.contract.regex-matchatworking-tree - topaz.decision.regex-byte-offsets.1 at
topaz.decisi
Report UTF-8 byte offsets for non-ASCII matches
Linkimport std.regex { Regex }import std.iofunction show(patternText: string, text: string) -> () {match (Regex.compile(patternText)) {case Ok(pattern) => {match (pattern.find(text)) {case Some(found) => io.writeLine("{found.start}:{found.end}:{found.text}")case None() => io.writeLine("no match")}}case Err(_) => io.writeLine("compile failed")}}show("한", "é한🙂")show("́", "éx")
2:5:한 1:3:́
Expected result
Expected result
Execution completes. Exit code 0.
2:5:한1:3:́
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 453 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 465 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 478 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 488 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 518 -
topaz.stdlib.contract.regex-matchatworking-tree - topaz.decision.regex-byte-offsets.1 at
topaz.decisi
Recover a match through the byte slice identified by its offsets
Linkimport std.regex { Regex }import std.iolet text = "한x"match (Regex.compile("한")) {case Ok(pattern) => {match (pattern.find(text)) {case Some(found) => {let bytes = text.encodeUtf8().slice(found.start, found.end)match (bytes.decodeUtf8()) {case Ok(decoded) => io.writeLine(decoded)case Err(_) => io.writeLine("invalid UTF-8 slice")}}case None() => io.writeLine("no match")}}case Err(_) => io.writeLine("compile failed")}
한
Expected result
Expected result
Execution completes. Exit code 0.
한
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 453 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 465 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 478 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 488 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 518 -
topaz.stdlib.contract.regex-matchatworking-tree - topaz.decision.regex-byte-offsets.1 at
topaz.decisi
Keep capture zero and distinguish missing and empty captures
Linkimport std.regex { Regex }import std.iomatch (Regex.compile("(?<word>a)(b)?()")) {case Ok(pattern) => {match (pattern.find("a")) {case Some(found) => {io.writeLine("{found.groups.length}")for group in (found.groups) {match (group) {case Some(text) => io.writeLine("some={text}")case None() => io.writeLine("none")}}}case None() => io.writeLine("no match")}}case Err(_) => io.writeLine("compile failed")}
4 some=a some=a none some=
Expected result
Expected result
Execution completes. Exit code 0.
4some=asome=anonesome=
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 453 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 465 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 478 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 488 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 518 -
topaz.stdlib.contract.regex-matchatworking-tree - topaz.decision.regex-byte-offsets.1 at
topaz.decisi
Omit absent named captures and retain a captured empty string
Linkimport std.regex { Regex }import std.iomatch (Regex.compile("(?<z>a)?(?<a>b*)")) {case Ok(pattern) => {match (pattern.find("")) {case Some(found) => {let mut snapshot = found.namedlet hasOptional = snapshot.containsKey("z")io.writeLine("{hasOptional}")match (snapshot.get("a")) {case Some(text) => io.writeLine("some={text}")case None() => io.writeLine("none")}snapshot.insert("a", "changed")match (found.named.get("a")) {case Some(text) => io.writeLine("again={text}")case None() => io.writeLine("none")}}case None() => io.writeLine("no match")}}case Err(_) => io.writeLine("compile failed")}
false some= again=
Expected result
Expected result
Execution completes. Exit code 0.
falsesome=again=
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 453 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 465 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 478 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 488 - compiler/crates/topaz_value/src/value/members.rs at
912a5e86e1f9· 518 -
topaz.stdlib.contract.regex-matchatworking-tree - topaz.decision.regex-byte-offsets.1 at
topaz.decisi