rex
Regular expressions - match the first occurrence of a pattern in a string, find all non-overlapping matches, replace matches with a substitution string, test whether a pattern is present, and split a string on a pattern (empty strings removed). Thin wrappers over the host-injected __rex_* natives.
Load with: use rex
What this module does
rex provides ilusm access to the embedder's regex engine via four
host-injected functions: __rex_mtc, __rex_all,
__rex_rep, and __rex_spl. The pattern syntax depends
on the host implementation (typically PCRE or ECMAScript regex).
Quick example
use rex
# First match
m = rexmt("\\d+", "order 42 has 7 items")
prn(m) # "42"
# All matches
all = rexal("\\d+", "order 42 has 7 items")
prn(all) # ["42", "7"]
# Replace
out = rexre("\\s+", "too many spaces", " ")
prn(out) # "too many spaces"
# Has (test for presence)
prn(rexha("^\\d", "42abc")) # tru
# Split on pattern
parts = rexsp("[,;\\s]+", "a, b; c d")
prn(parts) # ["a", "b", "c", "d"]
Functions
Matching and search
rexmt(pattern, string)Returns the first match of pattern in string, or nil if no match.
rexal(pattern, string)Returns a list of all non-overlapping matches of pattern in string.
rexha(pattern, string)Returns tru if pattern matches anywhere in string (coerced to string).
Replacement and splitting
rexre(pattern, string, replacement)Replaces matches of pattern in string with replacement. Returns the new string.
rexsp(pattern, string)Splits string on pattern. Empty strings in the result are filtered out.
Notes
- Requires the host to inject
__rex_mtc,__rex_all,__rex_rep, and__rex_spl. These are not available in all embedders. - Requires
trl.