Repository navigation
Regex
Frank edited this page Jan 26, 2026
·
2 revisions
CupidScript exposes regular expressions through the standard library. The engine is POSIX Extended Regular Expressions (ERE).
Regex is supported on POSIX builds (Linux/macOS). On Windows builds, regex functions may be unavailable.
Patterns follow POSIX ERE rules (examples):
-
[0-9]+- one or more digits -
[a-z]+- lowercase letters -
^...$- anchors for full-string matches -
( ... )- capturing groups
Returns true if any substring matches.
Returns true only if the entire string matches. Use ^ and $ to be explicit.
Returns the first match or nil if none. The match object contains:
-
start- byte offset of match start -
end- byte offset of match end -
match- the matched substring -
groups- list of capture groups (nilfor unmatched groups)
Returns all matches in order. Each entry is the same match object described above.
Replaces all matches with a literal replacement string. Capture expansions (like $1) are not supported.
let text = "User: alice42, email: alice@example.com";
print(regex_is_match("[0-9]+", text));
print(regex_match("^User: \\w+$", text));
let email = regex_find("([a-z]+)@([a-z]+\\.[a-z]+)", text);
if (email != nil) {
print(email["match"]);
print(email.groups[0]); // user
print(email.groups[1]); // domain
}
let nums = regex_find_all("[0-9]+", "x=7 y=42 z=105");
print(len(nums));
print(regex_replace("[a-z]+@[a-z]+\\.[a-z]+", text, "<hidden>"));Invalid patterns raise a runtime error. Wrap in try/catch if needed:
try {
regex_find("(", "text");
} catch (e) {
print("regex error:", e);
}