@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 |
Number of non-overlapping matches; 0 for an invalid pattern |
escape |
(s string) -> string |
Backslash-escape regex metacharacters so s matches literally |
find, find_all, find_groups, find_all_groups, replace, and split are fallible: single-variable assignment is a compile-time error (E3089); the result must be destructured (mut v, err = ... or mut v, _ = ...).
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:]].