lib/regex.spl)
Regular expression helpers implemented in pure SPL (backtracking matcher).
Supports a practical subset: .,
[classes] and negated [^classes],
(groups), named groups (?<name>…),
non-capturing (?:…), alternation |,
\d \s \w, backreferences \1–\9,
anchors ^ $,
quantifiers * + ? and non-greedy *? +? ??,
lookahead/lookbehind (?=…) (?!…)
(?<=…) (?<!…), and literal characters.
Load extensionless (reqFileExtension = False):
use regex;.
regex.match(pattern, string) — returns 1
if the entire string matches (re.fullmatch),
else 0.
regex.search(pattern, string) — index of the first match, or
-1 if none (re.search).
regex.findAll(pattern, string) — list of all non-overlapping matches
(re.findall). Capturing groups change what each list item contains.
regex.replace(pattern, repl, string) — substitute matches with
repl (re.sub).
regex.split(pattern, string) — split on the pattern; returns an SPL list of
parts (re.split).
regex.group(n) — text of capture group n
(1-based) from the last search or successful
match; empty string if none.
regex.groupNamed(name) — same for a
(?<name>…) group.
[^...] — negated class: matches one character not
in the class (same range/escape rules as [...]).
(pat) — capturing group (numbered 1, 2, …). Quantifiers after
) apply to the whole group, e.g.
(ab)+ matches abab.
(?<name>pat) — named capture; read with
regex.groupNamed("name") after
search.
(?:pat) — group without capturing.
\1 … \9 — backreference to a prior
capture in the pattern. In replace, use
\1 in the replacement string to insert the group text.
*?, +?,
?? (prefer the shortest match). Example:
^<.+?>$ matches <tag>.
(?=foo),
(?!foo), (?<=foo),
(?<!foo).
a|b — alternation: tries left branch first, then right (first match wins).
| inside [...] is literal.
^...$ with
regex.match (e.g.
^[a-z]+@[a-z]+\.[a-z]+$).
[0-9]+ inside
findAll to pull numbers from mixed text.
Use [^0-9]+ for non-digit runs.
0,
-1, [], or null
(no implicit coercion).
"\\d+", backref "\\1").
use regex;
test.assertTrue(regex.match("^\\d{3}-\\d{4}$", "555-0199"));
idx.setVar(regex.search("@", "user@host"));
test.assertEqualNumber(idx, 4);
nums.setVar(regex.findAll("[0-9]+", "a12b3"));
test.assertEqual(list.get(0, nums), "12");
clean.setVar(regex.replace("\\s+", " ", " hello world "));
test.assertEqual(clean, " hello world ");
parts.setVar(regex.split(",", "one,two,three"));
test.assertEqual(list.get(2, parts), "three");
test.assertTrue(regex.match("^[^0-9]+$", "abc"));
pet.setVar(regex.search("cat|dog", "my dog"));
test.assertEqualNumber(pet, 3);
test.assertTrue(regex.match("^(ab)+$", "abab"));
test.assertEqual(regex.replace("(ab|cd)", "X", "abcd"), "XX");
test.assertTrue(regex.match("^(a)\\1$", "aa"));
test.assertTrue(regex.match("^<.+?>$", ""));
regex.search("(?\\w+)", "hello");
test.assertEqual(regex.groupNamed("word"), "hello");
test.assertEqual(regex.replace("(a)", "X\\1X", "a"), "XaX");
Implementation reference: someProgrammingLanguage/lib/regex.spl.