veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

text/re

lib/text/re.vk

lib/text/re.vk Pure-Veko linear-time Thompson NFA regular expression engine (O(n * m) worst-case, zero backtracking). Supports . wildcard, ^ / $ anchors, *, +, ? quantifiers, | alternation, (...) grouping, character classes [a-z0-9_] / [^...], and \d, \D, \w, \W, \s, \S escapes. Zero capabilities required; works identically on VM, Wasm 3.0, and Native.

NfaInst struct
signature
NfaInst = struct { op i64, ch i64 = ..., out i64 = ..., out1 i64 = ..., cset bytes = ..., neg bool = ... }

Single compiled Thompson NFA instruction.

Regex struct
signature
Regex = struct { pattern str, insts block[NfaInst], start_pc i64 }

Compiled regular expression automaton.

Match struct
signature
Match = struct { start i64, end i64, text str }

Byte-span match result (start..end slice of the input string).

signature
compile = fn [pattern str] Regex

Compiles pattern into a linear-time Regex NFA (returns err on invalid syntax; combine with /or).

Executable Example
veko
use [re = text/re]
rx = re/compile 'v[0-9]+\\.[0-9]+'
ms = re/find-all rx 'release v1.0 and v2.14 ready'
if (len ms) != 2 or ms[0].text != 'v1.0' or ms[1].text != 'v2.14' [ err 're find-all' ] else [ .ok ]
signature
is-match? = fn [rx Regex, text str] bool

Returns true if rx matches anywhere inside text.

signature
match? = fn [pattern str, text str] bool

Convenience one-shot match test compiling pattern and testing text.

find fn
signature
find = fn [rx Regex, text str] Match

Finds the leftmost match of rx in text, or returns err if not found (combine with /or).

signature
find-all = fn [rx Regex, text str] block[Match]

Finds all non-overlapping matches of rx in text.

signature
replace-all = fn [rx Regex, text str, replacement str] str

Replaces all non-overlapping matches of rx in text with replacement.

signature
split = fn [rx Regex, text str] block[str]

Splits text around non-overlapping non-empty matches of rx.