ESC
Type to search...
S
Soli Docs

Regex Functions

Regular expression matching, finding, replacing, splitting, and capturing.

Pattern Matching

regex_match(pattern, string)

Test if a string matches a regex pattern.

Returns

Bool - true if the pattern matches, false otherwise
regex_match("^[a-z]+$", "hello")  # true
regex_match("^[0-9]+$", "hello")  # false
regex_match("\\d{3}-\\d{4}", "555-1234")  # true
let re = regex_new("^hello (\\w+)")

Check if a string matches a pattern.

if regex_match(re, "hello world") {
    println("Matched!")
}

Find the first match in a string.

let match = regex_find(re, "hello world")
println(match[0]) # "hello world"

Find all matches in a string.

let matches = regex_find_all(re, "hello world, hello universe")
println(matches[0][0]) # "hello world"
println(matches[1][0]) # "hello universe"

Replace matches in a string.

let result = regex_replace(re, "hello world", "goodbye $1")
println(result) # "goodbye world"

Replace all matches in a string.

let result = regex_replace_all(re, "hello world, hello universe", "hi $1")
println(result) # "hi world, hi universe"

Split a string by a pattern.

let parts = regex_split(re, "hello world, hello universe")
println(parts) # ["", ", ", ""]

Escape a string for use in a regex.

let escaped = regex_escape("hello.world")
println(escaped) # "hello\\.world"

Finding Matches

regex_find(pattern, string)

Find the first match in a string.

Returns

Hash? - { "match": String, "start": Int, "end": Int } or null if no match
let result = regex_find("[0-9]+", "abc123def")
println(result["match"])  # "123"
println(result["start"])  # 3
println(result["end"])    # 6
regex_find_all(pattern, string)

Find all matches in a string.

Returns

Array - Array of match objects
let all = regex_find_all("[0-9]+", "a1b2c3")
# [{"match": "1", "start": 1, "end": 2},
#  {"match": "2", "start": 3, "end": 4},
#  {"match": "3", "start": 5, "end": 6}]

Replacing

regex_replace(pattern, string, replacement)

Replace the first match.

regex_replace("[0-9]+", "a1b2", "X")  # "aXb2"
regex_replace_all(pattern, string, replacement)

Replace all matches.

regex_replace_all("[0-9]+", "a1b2", "X")  # "aXbX"

Splitting & Capturing

regex_split(pattern, string)

Split a string by a regex pattern.

regex_split("[,;]", "a,b;c")  # ["a", "b", "c"]
regex_split("\\s+", "hello   world")  # ["hello", "world"]
regex_capture(pattern, string)

Capture named groups from a pattern.

let result = regex_capture(
    "(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})",
    "2024-01-15"
)
println(result["year"])   # "2024"
println(result["month"])  # "01"
println(result["day"])    # "15"
regex_escape(string)

Escape special regex characters in a string.

regex_escape("hello.world")  # "hello\\.world"
regex_escape("(test)")       # "\\(test\\)"