regex module
ECMAScript-flavored regular expressions (powered by std::regex). Use for matching, capturing, replacement, and splitting.
import "regex" as regex;
print(regex.test("\\d+", "hello 42")); // true
print(regex.match("(\\w+)@(\\w+)", "[email protected]"));// ["[email protected]", "ada", "a"]
print(regex.replace("\\s+", "two spaces", "_"));// "two_spaces"
print(regex.split(",\\s*", "a, b,c")); // ["a", "b", "c"]
API
| Function | Description |
|---|---|
regex.test(pattern, s) → bool | True if pattern matches anywhere in s. |
regex.match(pattern, s) → list | null | First match as [full, group1, group2, ...], or null. |
regex.match_all(pattern, s) → list of lists | Every match. |
regex.replace(pattern, s, replacement) → string | Replace first match. Use \1, \2 in replacement to reference capture groups. |
regex.replace_all(pattern, s, replacement) → string | Replace every match. |
regex.split(pattern, s) → list | Split on each match. |
regex.escape(s) → string | Escape regex metacharacters so the result matches s literally. |
Notes
- Patterns are strings — escape backslashes when needed (
"\\d"for\d). - Bnlang has no regex literal syntax (no
/foo/g). All flags are baked into the pattern (e.g.(?i)for case-insensitive).