Skip to content
Systems programming for what’s next.
— Vex

Regex API (regex) ​

Regex — Compiled Pattern ​

MethodDescription
Regex.new(pattern)Compile pattern with default flags
Regex.compile(pattern, flags)Compile with explicit flags ("i", "m", "s")
Regex.invalid()Create an invalid/empty regex sentinel
.test(text): boolCheck if pattern matches anywhere in text
.isMatch(text): boolCheck if pattern matches the full text
.exec(text, &match): boolExecute and capture groups
.validCheck if compilation succeeded
.sourceOriginal pattern string
.groupCountNumber of capture groups

Invalid syntax, unknown flags or a pattern exceeding the documented resource budgets returns Regex.invalid() (valid == false). Compilation does not print to stdout.

Match — Captured Result ​

MethodDescription
Match.new()Create empty match
.value(): stringFull match text (group 0)
.group(i): Option<str>Get capture group by index
.groupCount(): i32Number of capture groups (excluding group 0)
.start(): i32Start position of full match
.end(): i32End position of full match

Match borrows the input text and owns reusable capture/scratch storage. group(i) returns a borrowed str; keep the original input alive and unchanged while using the match or its groups. Call toString() to retain an independent copy. value() produces an owned string. start() and end() are byte offsets (end-exclusive), with -1 when no full-match span exists; they are not Unicode-scalar or grapheme indexes.

Reuse one Match per concurrent caller; a later exec replaces its result. The compiled Regex remains immutable during matching. The engine operates on bytes, and case-insensitive matching is ASCII-oriented; use the Unicode package for Unicode normalization/case-fold policy before matching.

Flags ​

FlagInlineDescription
"i"(?i)Case-insensitive matching
"m"(?m)Multiline (^ and $ match line boundaries)
"s"(?s)Dot-all (. matches \n)

Only i, m and s are accepted by Regex.compile. Bare inline flags must appear at the beginning of the pattern. Scoped groups such as (?i:...) are currently rejected rather than being compiled with incorrect global semantics.

Examples ​

vex
import { Regex, Match } from "regex";

// Case-insensitive
let re = Regex.compile("hello", "i");
re.test("Hello World");  // true

// Capture groups
let re = Regex.new("(\\w+)@(\\w+)\\.(\\w+)");
let! m = Match.new();
if re.exec("user@example.com", &m!) {
    match m.group(1) {
        Some(user) => { $println(user); }, // user
        None => { $println("No capture"); },
    }
}

// Free functions
import { test, exec } from "regex";
test(&re, "some text");

Released under the MIT License.