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

regex

lib/regex.vk

lib/regex.vk Pure-Veko ReDoS-proof O(N * S) Thompson NFA regular expression engine. Supports literals, ., ^, $, *, +, ?, |, (...), [a-z], [^...], \d, \w, \s, and escapes. Zero capabilities required.

NfaState struct
signature
NfaState = struct { op i64, arg i64 = ..., out1 i64 = ..., out2 i64 = ... }

Single Thompson NFA state node.

CharClass struct
signature
CharClass = struct { negated bool = ..., ranges block = ... }

Character class specification (negated + list of [lo, hi] byte ranges).

Regex struct
signature
Regex = struct { pattern str, states block[NfaState], classes block[CharClass], start i64 }

Compiled Thompson NFA regular expression.

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

A matched substring span [start, end) within the input string.

signature
compile = fn [pattern str] Regex

Compiles a regular expression pattern into a linear-time Thompson NFA Regex.

Executable Example
veko
use [re = regex]
rx = re/compile '^[a-z]+-[0-9]+$'
if (not (re/is-match? rx 'veko-10')) or (re/is-match? rx 'Veko-10') [ err 'regex' ] else [ .ok ]
find fn
signature
find = fn [rx Regex, input str] Match

Finds the first match of rx anywhere in input, or returns err 'regex/find: no match'.

Executable Example
veko
use [re = regex]
rx = re/compile '\\d+'
m = re/find rx 'port=8443 ok'
if m.text != '8443' or m.start != 5 [ err 'regex/find' ] else [ .ok ]
signature
is-match? = fn [rx Regex, input str] bool

Returns true if rx matches anywhere inside input.

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

Finds all non-overlapping matches of rx in input.

Executable Example
veko
use [re = regex]
rx = re/compile '[a-z]+'
ms = re/find-all rx '12ab34cd56'
if (len ms) != 2 or ms[0].text != 'ab' or ms[1].text != 'cd' [ err 'regex/find-all' ] else [ .ok ]
signature
replace-all = fn [rx Regex, input str, replacement str] str

Replaces every non-overlapping match of rx in input with replacement.

Executable Example
veko
use [re = regex]
rx = re/compile '\\s+'
if (re/replace-all rx 'a   b\tc' '-') != 'a-b-c' [ err 'regex/replace-all' ] else [ .ok ]