Skip to content

@regex

Import this module with import @regex.

Regular expression operations using POSIX extended regex syntax.

Function Signature Description
is_valid (pattern string) -> bool Check if pattern is valid regex
is_match (pattern string, text string) -> bool Check if pattern matches text
find (pattern string, text string) -> (string, Error) First match — always use destructuring
find_all (pattern string, text string) -> ([string], Error) All matches — always use destructuring
find_groups (pattern string, text string) -> ([string], Error) Capture groups of the first match (index 0 is the whole match) — always use destructuring
find_all_groups (pattern string, text string) -> ([[string]], Error) Capture groups for every match — always use destructuring
replace (pattern string, text string, replacement string) -> (string, Error) Replace matches — always use destructuring
split (pattern string, text string) -> ([string], Error) Split by pattern — always use destructuring
count (pattern string, text string) -> (i64, Error) Number of non-overlapping matches — always use destructuring
escape (s string) -> string Backslash-escape regex metacharacters so s matches literally

find, find_all, find_groups, find_all_groups, replace, split, and count are fallible: destructure the result (mut v, err = ... or mut v, _ = ...).

On every platform, . and a bracket expression match one character (a codepoint), not one byte: ^.$ matches é, ^..$ does not, and [é] matches é and nothing else. An invalid UTF-8 byte in the text is one U+FFFD character: it matches . and a negated bracket expression, and it never makes the match fail.POSIX ERE has no Perl-style shorthand classes. \d, \w, \s, \b (and \D, \W, \S, \B) are rejected as invalid patterns — use the POSIX bracket classes instead: [[:digit:]], [[:alnum:]_], [[:space:]]. The classes match ASCII characters only, as in the chars module; [[:alpha:]] does not match é.