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 byregex.compileon syntax or complexity-limit errors ("syntax_error"or"limit_exceeded").
Functions & Methods
| Symbol | Signature | Description |
|---|---|---|
regex.compile | regex.compile(pattern: String) -> Regex | RegexError | Compiles pattern into a Thompson NFA. |
Regex.is_match | re.is_match(text: String) -> Bool | Returns true if text matches anywhere. |
Regex.find | re.find(text: String) -> Match? | Returns the leftmost-first match, or nil. |
Regex.find_all | re.find_all(text: String) -> [Match] | Returns all non-overlapping matches in order. |
Regex.captures | re.captures(text: String) -> [String?]? | Returns capture groups [whole, g1, g2, ...] for the first match (nil for unmatched optional groups). |
Regex.replace | re.replace(text: String, rep: String) -> String | Replaces the first match with literal rep. |
Regex.replace_all | re.replace_all(text: String, rep: String) -> String | Replaces all non-overlapping matches with literal rep. |
Regex.split | re.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
8192bytes, nesting depth64, capture groups64, counted repetition256, NFA states4096.
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}$")
}