Standard Library
std.regex
Regular expression pattern matching and text manipulation.
Exported functions
pub def compile(pattern: String) -> Int
Compile a regex pattern and return a handle.
Examples
const re = regex.compile("\\d+")
const email_re = regex.compile("[a-z]+@[a-z]+\\.[a-z]+")pub def is_match(handle: Int, text: String) -> Bool
Test if the pattern matches anywhere in the text.
Examples
const re = regex.compile("\\d+")
regex.is_match(re, "abc123") # true
regex.is_match(re, "abcdef") # falsepub def find_all(handle: Int, text: String) -> List[String]
Find all non-overlapping matches as a list of strings.
Examples
const re = regex.compile("\\d+")
const nums = regex.find_all(re, "foo42bar7baz")
# nums = ["42", "7"]pub def captures(handle: Int, text: String) -> List[String]
Get capture groups from the first match.
Examples
const re = regex.compile("(\\d+)-(\\d+)")
const caps = regex.captures(re, "foo12-34bar")
# caps = ["12-34", "12", "34"]pub def replace(handle: Int, text: String, replacement: String) -> String
Replace the first match with a replacement string.
Examples
const re = regex.compile("world")
regex.replace(re, "hello world world", "universe")
# "hello universe world"
const re = regex.compile("(\\w+) (\\w+)")
regex.replace(re, "hello world", "$2 $1")
# "world hello"pub def replace_all(handle: Int, text: String, replacement: String) -> String
Replace all matches with a replacement string.
Examples
const re = regex.compile("\\d+")
regex.replace_all(re, "foo42bar7baz", "X")
# "fooXbarXbaz"pub def split(handle: Int, text: String) -> List[String]
Split the text by the pattern into a list of strings.
Examples
const re = regex.compile("[,;]\\s*")
const parts = regex.split(re, "a, b; c,d")
# parts = ["a", "b", "c", "d"]