silk.text.regex.
Native ECMAScript Unicode regular expressions for text and JSON Schema patterns.
When to use#
Use Regex.compile and Regex.test to reuse a Pattern across many searches. Use
Regex.validate to check syntax or Regex.matches for a one-off unanchored search. Patterns
use the ECMA-262 Unicode (u) grammar, not shell glob syntax.
Details#
Matching supports captures, named and numeric backreferences, forward and backward assertions,
alternation, character classes, Unicode properties, greedy and lazy repetition, and scoped
i, m, and s modifiers. Unicode properties and simple case folding use Unicode 17.0.0.
Text is UTF-8: positions advance by Unicode scalars, never by bytes or UTF-16 code units.
Syntax failures and exhausted resource limits are errors, never a negative match.
Import as Regex with import silk.text.regex { Regex }.
Public declarations: 4.
RegexReason#
pub enum RegexReasonThe reason a Unicode regular expression could not be parsed or matched.
UnexpectedToken#
UnexpectedTokenA metacharacter occurs where the Unicode pattern grammar does not permit it.
UnclosedGroup#
UnclosedGroupA parenthesized group has no closing parenthesis.
UnclosedClass#
UnclosedClassA character class has no closing bracket.
InvalidEscape#
InvalidEscapeAn escape is incomplete or forbidden by the Unicode pattern grammar.
InvalidRange#
InvalidRangeA class range has non-character endpoints or descending endpoints.
InvalidQuantifier#
InvalidQuantifierA quantifier is malformed, has descending bounds, or follows an assertion.
InvalidReference#
InvalidReferenceA numeric or named backreference has no corresponding capture.
InvalidName#
InvalidNameA capture name is not an ECMAScript identifier.
DuplicateName#
DuplicateNameTwo same-named captures can participate in the same alternative.
InvalidProperty#
InvalidPropertyA Unicode property name or value is not an exact permitted alias.
PatternLimit#
PatternLimitThe pattern exceeds a documented parser size, nesting, or repetition bound.
MatchLimit#
MatchLimitMatching exhausted the documented work or recursive-frame limit.
RegexError#
pub struct RegexErrorA structured regex failure, distinct from a valid pattern that does not match.
Field reason#
pub reason: RegexReasonThe syntax or resource-limit failure.
Field offset#
pub offset: usizeThe byte offset in the pattern where the failure was detected; zero for a matching limit.
Pattern#
pub struct PatternAn owned, immutable ECMAScript Unicode regular-expression program.
Details#
Construct it with Regex.compile and search any number of strings with Regex.test. It owns
decoded instructions and does not borrow the source pattern. Matching does not mutate it.
Regex#
pub struct RegexThe owner of native ECMAScript Unicode pattern validation and unanchored matching operations.
Implementation Regex#
impl RegexAssociated function compile#
pub effect fn compile(pattern: string) -> Pattern ! RegexError | OutOfMemoryError ? &mut AllocatorCompiles an ECMAScript Unicode (u) pattern for repeated unanchored searches.
Details#
Patterns have no surrounding slashes or flags. Scoped i, m and s modifiers are supported.
Captures, backreferences, lookarounds and Unicode property escapes are compiled natively;
property aliases and simple case folding use Unicode 17.0.0. The result owns its instructions
and does not borrow pattern.
Errors#
RegexErrorfor invalid syntax, more than 65,536 pattern bytes, group nesting beyond 256, or a repetition bound greater than 1,000,000.OutOfMemoryErrorif the allocator cannot supply the program.
Associated function validate#
pub effect fn validate(pattern: string) -> () ! RegexError | OutOfMemoryError ? &mut AllocatorChecks the entire pattern against the ECMAScript Unicode (u) grammar.
Details#
There are no surrounding slashes or flags. Scoped (?ims-ims:...) modifiers are accepted.
Unicode property aliases are exact, case-sensitive Unicode 17.0.0 aliases. Numeric and named
forward references are checked after parsing; duplicate names are allowed only in disjoint
alternatives. Legacy octal and identity escapes are not accepted in Unicode mode.
Errors#
RegexErrorfor invalid syntax, more than 65,536 pattern bytes, nesting beyond 256 groups, or a repetition bound greater than 1,000,000.OutOfMemoryErrorif the allocator cannot supply the parsed pattern.
Associated function matches#
pub effect fn matches(pattern: string, text: string) -> bool
! RegexError | OutOfMemoryError ? &mut AllocatorReturns whether any substring of text matches an ECMAScript Unicode (u) pattern.
Details#
The pattern has no surrounding slashes or flags. Matching is case-sensitive, with . excluding
LF, CR, U+2028 and U+2029, unless scoped i, m or s modifiers change those rules. Without
m, ^ and $ mean the actual beginning and end, including when text ends in a newline.
\d and \w are ASCII classes; \s is ECMAScript whitespace and line terminators.
Captures, backreferences, lazy repetition and variable-length lookbehind have ECMAScript
backtracking order. Positive lookarounds commit their first successful captures. The Unicode
property and case-folding data version is 17.0.0. Text advances by UTF-8 Unicode scalars.
Errors#
RegexErrorfor invalid syntax, more than 65,536 pattern bytes, group nesting beyond 256, a repetition bound greater than 1,000,000, more than 1,000,000 matcher steps, or recursive matching deeper than 1,024 frames. Exhaustion is never reported asfalse.OutOfMemoryErrorif the allocator cannot supply the program or matcher state.
Associated function test#
pub effect fn test(pattern: &Pattern, text: string) -> bool
! RegexError | OutOfMemoryError ? &mut AllocatorSearches text with a previously compiled ECMAScript Unicode pattern.
Details#
Searches are unanchored unless the pattern supplies anchors. Captures and repetition use ECMAScript backtracking order, positive lookarounds are atomic, and lookbehind runs backward. Text advances by Unicode scalars. The pattern is immutable; input and backtracking storage belong to this invocation. Unicode properties and folding use Unicode 17.0.0.
Errors#
RegexErrorwithMatchLimitafter 1,000,000 matcher steps or 1,024 recursive frames. Exhaustion is never reported asfalse.OutOfMemoryErrorif the allocator cannot supply input or matcher storage.
Category: Text.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.