Skip to content

cdt.regex ​

Zero-dependency linear-time regular expression engine backed by a Thompson NFA / PikeVM (O(N * M) guaranteed matching time, immune to ReDoS / catastrophic backtracking).

Import ​

conduit
import * as regex from "cdt.regex"

Types ​

  • Regex — Opaque compiled pattern handle (.is_match, .find, .find_all, .captures, .replace, .replace_all, .split).
  • Match { text: String, start: Int, end: Int } — Matched substring along with UTF-8 byte offsets [start, end).
  • RegexError { kind: String, message: String } — Returned by regex.compile on syntax or complexity-limit errors ("syntax_error" or "limit_exceeded").

Functions & Methods ​

SymbolSignatureDescription
regex.compileregex.compile(pattern: String) -> Regex | RegexErrorCompiles pattern into a Thompson NFA.
Regex.is_matchre.is_match(text: String) -> BoolReturns true if text matches anywhere.
Regex.findre.find(text: String) -> Match?Returns the leftmost-first match, or nil.
Regex.find_allre.find_all(text: String) -> [Match]Returns all non-overlapping matches in order.
Regex.capturesre.captures(text: String) -> [String?]?Returns capture groups [whole, g1, g2, ...] for the first match (nil for unmatched optional groups).
Regex.replacere.replace(text: String, rep: String) -> StringReplaces the first match with literal rep.
Regex.replace_allre.replace_all(text: String, rep: String) -> StringReplaces all non-overlapping matches with literal rep.
Regex.splitre.split(text: String) -> [String]Splits text around matches of re.

Supported Syntax & Limits ​

  • Atoms: literal UTF-8 characters, . (any char except \n), character classes [a-z0-9], negated classes [^...], escapes (\n, \r, \t, \\, punctuation \. \* \[ etc.), shorthands \d, \D, \w, \W, \s, \S (both outside and inside [...]).
  • Anchors: ^, $, \b, \B.
  • Groups & Alternation: |, capturing groups (...), non-capturing groups (?:...).
  • Quantifiers (greedy and lazy *?, +?, ??, {m,n}?): *, +, ?, {m}, {m,}, {m,n}.
  • Safety Limits: max pattern 8192 bytes, nesting depth 64, capture groups 64, counted repetition 256, NFA states 4096.

Integration with cdt.validation & OpenAPI ​

Fields typed as String can declare @validation.regex("...") (validated at compile time) or call validation.regex(val, pattern) -> Issue?. When used in HTTP request body structs, conduit openapi automatically exports the pattern as JSON Schema "pattern":

conduit
import * as regex from "cdt.regex"
import * as validation from "cdt.validation"

struct UserInput {
    handle: String @validation.regex("^[a-z0-9_]{3,16}$")
}