Skip to content

Regular Expressions (std/regex) ​

milo
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 ​

milo
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 ​

milo
pub struct Regex

A compiled POSIX extended regular expression. Compile once and reuse the value.

Regex.compile ​

milo
fn Regex.compile(pattern: string): Result<Regex>

Compile a POSIX extended regular expression.

Regex.compileFlags ​

milo
fn Regex.compileFlags(pattern: string, flags: i32): Result<Regex>

Compile with explicit POSIX cflags: REG_EXTENDED=1, REG_ICASE=2.

Regex.find ​

milo
fn Regex.find(self: &Regex, input: &string): Option<RegexMatch>

The first match, or None. start and end are byte offsets into input.

Regex.findAll ​

milo
fn Regex.findAll(self: &Regex, input: &string): Vec<RegexMatch>

Every non-overlapping match, left to right.

Regex.isMatch ​

milo
fn Regex.isMatch(self: &Regex, input: &string): bool

Whether the pattern matches anywhere in input.

RegexMatch ​

milo
pub struct RegexMatch

Match result: start and end byte offsets.

Fields: start: i64, end: i64.