radlang — Regular expressions
The regex namespace compiles POSIX extended regular expressions into a
RegEx value.
RegEx re = regex.from("hello[world]", "i")
bool matched = re.isMatch("HELLOw")
string? first = re.find("say HELLOw")
string replaced = re.replace("say HELLOw there", "hi") // "say hi there"
re.close() API
fn regex.from(string pattern, string flags?): RegEx
fn RegEx.isMatch(string text): bool
fn RegEx.find(string text): string?
fn RegEx.match(string text): string[]?
fn RegEx.findAll(string text): string[]
fn RegEx.split(string text): string[]
fn RegEx.replace(string text, string replacement): string
fn RegEx.replaceAll(string text, string replacement): string
fn RegEx.close(): voidFlags: i ignore case, m newline-sensitive (^/$ match at line
boundaries, . and bracket expressions do not match newline), g make
replace replace every match. Release a compiled expression with close(), or
declare it as a using resource: using (RegEx r = regex.from("a+")) { ... }.
regex.from throws a catchable Error for an invalid pattern (for example
regex: parentheses not balanced).
find returns null when there is no match. A regex literal such as
/[0-9]+/g is shorthand for regex.from("[0-9]+", "g").
Capture groups
match returns the first match as a list: index 0 is the whole match and
indexes 1.. are the parenthesised groups in order. A group that did not take
part in the match (an unmatched optional group) is the empty string, so the
list always has groups + 1 entries. It returns null when the text does not
match.
RegEx date = regex.from("([0-9]+)-([0-9]+)-([0-9]+)")
string[]? m = date.match("on 2026-11-07 ok")
if (m != null) {
sys.output(m[0]) // 2026-11-07
sys.output(m[1]) // 2026
sys.output(m[3]) // 07
} findAll and split
findAll returns every non-overlapping match, left to right, regardless of the
g flag. split returns the text between matches (capture groups are not
included in the result); leading and trailing empty strings are kept, and text
with no match yields a one-element list.
RegEx digits = regex.from("[0-9]+")
string[] all = digits.findAll("a1 b22 c333") // ["1", "22", "333"]
RegEx sep = regex.from("[,;] *")
string[] parts = sep.split("a, b;c, d") // ["a", "b", "c", "d"] replace and replaceAll
replace keeps its original behaviour: it replaces the first match, or
every match when the expression was compiled with the g flag, and inserts the
replacement text literally (no backreferences).
replaceAll always replaces every match (the g flag is irrelevant) and
expands backreferences in the replacement:
| Token | Inserts |
|---|---|
$0 |
the whole match |
$1..$9 |
that capture group (empty if the group did not match or does not exist) |
$$ |
a literal $ |
Any other $ is kept as-is.
RegEx kv = regex.from("([a-z]+)=([0-9]+)")
string swapped = kv.replaceAll("a=1 b=22", "$2:$1") // "1:a 22:b" Empty matches and UTF-8
A pattern that can match the empty string (x*) matches once at every
character boundary, and after an empty match the scan advances by a whole UTF-8
code point, so a multi-byte character is never split between iterations:
RegEx empty = regex.from("x*")
string r = empty.replaceAll("abc", "-") // "-a-b-c-"
string[] chars = empty.split("abc") // ["a", "b", "c"]Offsets are byte offsets and the matcher is the C library’s POSIX engine, which
works byte-wise in the default locale: . matches one byte, not one code
point, so prefer patterns made of literal UTF-8 text over . when the text may
contain non-ASCII characters. When a search continues after an earlier match,
^ anchors only at the real start of the text (and, with m, after a newline).