Regex
Regex module: simple regular expression engine (pure March implementation).
Supports: literal chars exact character match . any single character
- zero or more of preceding atom (greedy)
+ one or more of preceding atom (greedy) ? zero or one of preceding atom (greedy) ^ anchor at start of string $ anchor at end of string [abc] character class (any of a, b, c) [a-z] character range in a class [^abc] negated character class \d \D digit / non-digit \w \W word char (alphanumeric + _) / non-word \s \S whitespace / non-whitespace \\ \. \* etc. literal special characters
Functions: match(pattern, string) -> Bool find(pattern, string) -> Option(String) find_all(pattern, string) -> List(String) replace(pattern, replacement, string) -> String split(pattern, string) -> List(String)
Types
Functions
Test whether pattern matches anywhere in string. If pattern starts with '^', the match is anchored to the start. If pattern ends with '$', the match is anchored to the end.
Regex.matches("\\d+", "hello 42 world") -- true Regex.matches("^hello", "hello world") -- true Regex.matches("^hello", "say hello") -- false
Find the first match of pattern in string. Returns Some(matched_substring) or None.
Regex.find("\\d+", "price: 42 dollars") -- Some("42") Regex.find("xyz", "hello") -- None
Find all non-overlapping matches of pattern in string. Returns a list of matched substrings in order.
Regex.find_all("\\d+", "a1 b22 c333") -- ["1", "22", "333"]
Replace the first match of pattern in string with replacement. Returns the resulting string.
Regex.replace("\\d+", "NUM", "price 42") -- "price NUM"
Replace all non-overlapping matches of pattern in string with replacement. Returns the resulting string.
Regex.replace_all("\\d", "X", "a1b2c3") -- "aXbXcX"
Split string on matches of pattern. Returns the list of substrings between matches.
Regex.split("\\s+", "hello world foo") -- ["hello", "world", "foo"] Regex.split(",", "a,b,c") -- ["a", "b", "c"]