split(text: string) - > Array<string>- Module
- std.
regex - Receiver
- Regex
- Minimum arity
- 1
- Maximum arity
- 1
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· 1067 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 55 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1067 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 442 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 275
Receiver
Regex
Parameter constraints
Regex.split returns the input portions between the matches selected by findAll. Delimiters disappear and capture groups are not inserted. Empty portions at either end and between adjacent delimiters are retained. No match produces an array containing the original input.
Regex.replaceAll replaces the same nonoverlapping matches while copying all text between them unchanged. No match returns the original text. The operation does not search inserted replacement text.
Replacement references use a dollar sign followed by a braced capture number or name. Capture zero denotes the complete match. An unknown or nonparticipating capture expands to an empty string. Two dollar signs produce one literal dollar.
A replacement selector is a nonnegative decimal capture number or an ASCII capture name. Dollar text that is neither a double dollar nor a well-formed braced selector is literal. Unbraced dollar names and numbers do not form references. Expansion is performed once and captured dollar text is not expanded again.
An empty pattern splits at every scalar boundary and inserts replacement text at those same boundaries. The adjacent-empty-match rule from findAll also applies when a nonempty match is followed by an empty candidate.
The receiver is evaluated once before text and replacement arguments, which are evaluated once from left to right. Splitting and replacement leave the original string and Regex unchanged. They have no external effects and do not call a user callback.
Related rule: Split and replace using the shared match sequenceResult
The declared result type is Array<string>.
Effects
Failures
Result and effects
LinkReturns the input portions between selected matches and retains empty portions.
Resolved contracts
LinkRelated entries
LinkSplit and replace using the shared match sequence
LinkSplit and replace using the shared match sequence
Regex.split returns the input portions between the matches selected by findAll. Delimiters disappear and capture groups are not inserted. Empty portions at either end and between adjacent delimiters are retained. No match produces an array containing the original input.
Regex.replaceAll replaces the same nonoverlapping matches while copying all text between them unchanged. No match returns the original text. The operation does not search inserted replacement text.
Replacement references use a dollar sign followed by a braced capture number or name. Capture zero denotes the complete match. An unknown or nonparticipating capture expands to an empty string. Two dollar signs produce one literal dollar.
A replacement selector is a nonnegative decimal capture number or an ASCII capture name. Dollar text that is neither a double dollar nor a well-formed braced selector is literal. Unbraced dollar names and numbers do not form references. Expansion is performed once and captured dollar text is not expanded again.
An empty pattern splits at every scalar boundary and inserts replacement text at those same boundaries. The adjacent-empty-match rule from findAll also applies when a nonempty match is followed by an empty candidate.
The receiver is evaluated once before text and replacement arguments, which are evaluated once from left to right. Splitting and replacement leave the original string and Regex unchanged. They have no external effects and do not call a user callback.
Why
Using one match sequence prevents a search from disagreeing with a later replacement. Braced capture references have one spelling and an explicit end, so a capture name cannot absorb following replacement text.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 576 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 607 - compiler/crates/topaz_product_runtime/src/runtime/call.rs at
912a5e86e1f9· 1019 -
topaz.stdlib.contract.regex-transformatworking-tree - topaz.decision.regex-capture-replacement.1 at
topaz.decisi
Retain empty split fields without inserting captures
Linkimport std.regex { Regex }import std.iomatch (Regex.compile("(,)")) {case Ok(pattern) => {for field in (pattern.split(",a,,b,")) {io.writeLine("[{field}]")}for field in (pattern.split("plain")) {io.writeLine("[{field}]")}}case Err(_) => io.writeLine("compile failed")}
[] [a] [] [b] [] [plain]
Expected result
Expected result
Execution completes. Exit code 0.
[][a][][b][][plain]
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 576 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 607 - compiler/crates/topaz_product_runtime/src/runtime/call.rs at
912a5e86e1f9· 1019 -
topaz.stdlib.contract.regex-transformatworking-tree - topaz.decision.regex-capture-replacement.1 at
topaz.decisi
Expand braced capture references and a literal dollar
Linkimport std.regex { Regex }import std.iomatch (Regex.compile("(?<word>[a-z]+)([0-9]+)?")) {case Ok(pattern) => {let replacement = "$\{word\}/$\{0\}/$\{2\}/$\{missing\}/$$"io.writeLine(pattern.replaceAll("a1 b", replacement))io.writeLine(pattern.replaceAll("123", replacement))}case Err(_) => io.writeLine("compile failed")}
a/a1/1//$ b/b///$ 123
Expected result
Expected result
Execution completes. Exit code 0.
a/a1/1//$ b/b///$123
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 576 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 607 - compiler/crates/topaz_product_runtime/src/runtime/call.rs at
912a5e86e1f9· 1019 -
topaz.stdlib.contract.regex-transformatworking-tree - topaz.decision.regex-capture-replacement.1 at
topaz.decisi
Preserve text that is not a braced reference
Linkimport std.regex { Regex }import std.iomatch (Regex.compile("a")) {case Ok(pattern) => io.writeLine(pattern.replaceAll("a", "$word $1 $ $\{\} $\{bad-name\}"))case Err(_) => io.writeLine("compile failed")}
$word $1 $ ${} ${bad-name}
Expected result
Expected result
Execution completes. Exit code 0.
$word $1 $ ${} ${bad-name}
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 576 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 607 - compiler/crates/topaz_product_runtime/src/runtime/call.rs at
912a5e86e1f9· 1019 -
topaz.stdlib.contract.regex-transformatworking-tree - topaz.decision.regex-capture-replacement.1 at
topaz.decisi
Split and replace at each empty-pattern boundary
Linkimport std.regex { Regex }import std.iomatch (Regex.compile("")) {case Ok(pattern) => {for field in (pattern.split("ab")) {io.writeLine("[{field}]")}io.writeLine(pattern.replaceAll("ab", "-"))}case Err(_) => io.writeLine("compile failed")}
[] [a] [b] [] -a-b-
Expected result
Expected result
Execution completes. Exit code 0.
[][a][b][]-a-b-
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 576 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 607 - compiler/crates/topaz_product_runtime/src/runtime/call.rs at
912a5e86e1f9· 1019 -
topaz.stdlib.contract.regex-transformatworking-tree - topaz.decision.regex-capture-replacement.1 at
topaz.decisi
Share the adjacent-empty-match rule across transformations
Linkimport std.regex { Regex }import std.iomatch (Regex.compile("a*")) {case Ok(pattern) => {for field in (pattern.split("ab")) {io.writeLine("[{field}]")}io.writeLine(pattern.replaceAll("ab", "-"))}case Err(_) => io.writeLine("compile failed")}
[] [b] [] -b-
Expected result
Expected result
Execution completes. Exit code 0.
[][b][]-b-
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 576 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 607 - compiler/crates/topaz_product_runtime/src/runtime/call.rs at
912a5e86e1f9· 1019 -
topaz.stdlib.contract.regex-transformatworking-tree - topaz.decision.regex-capture-replacement.1 at
topaz.decisi
Evaluate receiver and replacement inputs once in source order
Linkimport std.regex { Regex }import std.testimport std.iofunction makePattern() -> Regex {io.writeLine("receiver")return test.assertOk(Regex.compile("a"))}function inputText() -> string {io.writeLine("text")return "a"}function replacementText() -> string {io.writeLine("replacement")return "b"}io.writeLine(makePattern().replaceAll(inputText(), replacementText()))
receiver text replacement b
Expected result
Expected result
Execution completes. Exit code 0.
receivertextreplacementb
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 576 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 607 - compiler/crates/topaz_product_runtime/src/runtime/call.rs at
912a5e86e1f9· 1019 -
topaz.stdlib.contract.regex-transformatworking-tree - topaz.decision.regex-capture-replacement.1 at
topaz.decisi