Regular Expressions (std/regex)
from "std/regex" import { Regex, RegexMatch }Regex is a compiled regular expression. Compilation is fallible; searching a valid expression may simply find no match. Keeping those outcomes separate is the module's error model.
Example
let re = Regex.compile("[0-9]+")?
if re.isMatch("abc123") {
let first = re.find("abc123")!
print("abc123"[first.start..first.end])
}macOS and Linux use POSIX regex. On Windows the module fails loudly until the pure-Milo engine lands.
API reference
std/regex has one implementation per target (darwin, linux, windows); the resolver picks the one for the target you build for.
Regex
pub struct RegexA compiled POSIX extended regular expression. Compile once and reuse the value.
Regex.compile
fn Regex.compile(pattern: string): Result<Regex>Compile a POSIX extended regular expression.
Regex.compileFlags
fn Regex.compileFlags(pattern: string, flags: i32): Result<Regex>Compile with explicit POSIX cflags: REG_EXTENDED=1, REG_ICASE=2.
Regex.find
fn Regex.find(self: &Regex, input: &string): Option<RegexMatch>The first match, or None. start and end are byte offsets into input.
Regex.findAll
fn Regex.findAll(self: &Regex, input: &string): Vec<RegexMatch>Every non-overlapping match, left to right.
Regex.isMatch
fn Regex.isMatch(self: &Regex, input: &string): boolWhether the pattern matches anywhere in input.
RegexMatch
pub struct RegexMatchMatch result: start and end byte offsets.
Fields: start: i64, end: i64.