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
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\\)"