Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · std.regex

    Regex.split

    Returns the input portions between selected matches and retains empty portions.

    split(text: string) -> Array<string>
    Module
    std.regex
    Receiver
    Regex
    Minimum arity
    1
    Maximum arity
    1

    Declaration and parameters

    Link

    Availability

    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

    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.

    Result

    The declared result type is Array<string>.

    Effects

    Failures

    Result and effects

    Link

    Returns the input portions between selected matches and retains empty portions.

    Resolved contracts

    Link

    Related entries

    Link

    std.regex

    Split and replace using the shared match sequence

    Link
    Ruletopaz.stdlib.contract.regex-transform.rule

    Split 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

    Retain empty split fields without inserting captures

    Link
    main.tpzRetain empty split fields without inserting capturesOpen in Playground
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("(,)")) {
    4. case Ok(pattern) => {
    5. for field in (pattern.split(",a,,b,")) {
    6. io.writeLine("[{field}]")
    7. }
    8. for field in (pattern.split("plain")) {
    9. io.writeLine("[{field}]")
    10. }
    11. }
    12. case Err(_) => io.writeLine("compile failed")
    13. }
    Expected outputexit 0
    []
    [a]
    []
    [b]
    []
    [plain]
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. []
    2. [a]
    3. []
    4. [b]
    5. []
    6. [plain]
    Source evidence

    Expand braced capture references and a literal dollar

    Link
    main.tpzExpand braced capture references and a literal dollarOpen in Playground
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("(?<word>[a-z]+)([0-9]+)?")) {
    4. case Ok(pattern) => {
    5. let replacement = "$\{word\}/$\{0\}/$\{2\}/$\{missing\}/$$"
    6. io.writeLine(pattern.replaceAll("a1 b", replacement))
    7. io.writeLine(pattern.replaceAll("123", replacement))
    8. }
    9. case Err(_) => io.writeLine("compile failed")
    10. }
    Expected outputexit 0
    a/a1/1//$ b/b///$
    123
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. a/a1/1//$ b/b///$
    2. 123
    Source evidence

    Preserve text that is not a braced reference

    Link
    main.tpzPreserve text that is not a braced referenceOpen in Playground
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("a")) {
    4. case Ok(pattern) => io.writeLine(pattern.replaceAll("a", "$word $1 $ $\{\} $\{bad-name\}"))
    5. case Err(_) => io.writeLine("compile failed")
    6. }
    Expected outputexit 0
    $word $1 $ ${} ${bad-name}
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. $word $1 $ ${} ${bad-name}
    Source evidence

    Split and replace at each empty-pattern boundary

    Link
    main.tpzSplit and replace at each empty-pattern boundaryOpen in Playground
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("")) {
    4. case Ok(pattern) => {
    5. for field in (pattern.split("ab")) {
    6. io.writeLine("[{field}]")
    7. }
    8. io.writeLine(pattern.replaceAll("ab", "-"))
    9. }
    10. case Err(_) => io.writeLine("compile failed")
    11. }
    Expected outputexit 0
    []
    [a]
    [b]
    []
    -a-b-
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. []
    2. [a]
    3. [b]
    4. []
    5. -a-b-
    Source evidence

    Share the adjacent-empty-match rule across transformations

    Link
    main.tpzShare the adjacent-empty-match rule across transformationsOpen in Playground
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("a*")) {
    4. case Ok(pattern) => {
    5. for field in (pattern.split("ab")) {
    6. io.writeLine("[{field}]")
    7. }
    8. io.writeLine(pattern.replaceAll("ab", "-"))
    9. }
    10. case Err(_) => io.writeLine("compile failed")
    11. }
    Expected outputexit 0
    []
    [b]
    []
    -b-
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. []
    2. [b]
    3. []
    4. -b-
    Source evidence

    Evaluate receiver and replacement inputs once in source order

    Link
    main.tpzEvaluate receiver and replacement inputs once in source orderOpen in Playground
    1. import std.regex { Regex }
    2. import std.test
    3. import std.io
    4. function makePattern() -> Regex {
    5. io.writeLine("receiver")
    6. return test.assertOk(Regex.compile("a"))
    7. }
    8. function inputText() -> string {
    9. io.writeLine("text")
    10. return "a"
    11. }
    12. function replacementText() -> string {
    13. io.writeLine("replacement")
    14. return "b"
    15. }
    16. io.writeLine(makePattern().replaceAll(inputText(), replacementText()))
    Expected outputexit 0
    receiver
    text
    replacement
    b
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. receiver
    2. text
    3. replacement
    4. b
    Source evidence

    Menu

    Download