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#

silk
pub enum RegexReason

The reason a Unicode regular expression could not be parsed or matched.

UnexpectedToken#

silk
UnexpectedToken

A metacharacter occurs where the Unicode pattern grammar does not permit it.

UnclosedGroup#

silk
UnclosedGroup

A parenthesized group has no closing parenthesis.

UnclosedClass#

silk
UnclosedClass

A character class has no closing bracket.

InvalidEscape#

silk
InvalidEscape

An escape is incomplete or forbidden by the Unicode pattern grammar.

InvalidRange#

silk
InvalidRange

A class range has non-character endpoints or descending endpoints.

InvalidQuantifier#

silk
InvalidQuantifier

A quantifier is malformed, has descending bounds, or follows an assertion.

InvalidReference#

silk
InvalidReference

A numeric or named backreference has no corresponding capture.

InvalidName#

silk
InvalidName

A capture name is not an ECMAScript identifier.

DuplicateName#

silk
DuplicateName

Two same-named captures can participate in the same alternative.

InvalidProperty#

silk
InvalidProperty

A Unicode property name or value is not an exact permitted alias.

PatternLimit#

silk
PatternLimit

The pattern exceeds a documented parser size, nesting, or repetition bound.

MatchLimit#

silk
MatchLimit

Matching exhausted the documented work or recursive-frame limit.

RegexError#

silk
pub struct RegexError

A structured regex failure, distinct from a valid pattern that does not match.

Field reason#

silk
pub reason: RegexReason

The syntax or resource-limit failure.

Field offset#

silk
pub offset: usize

The byte offset in the pattern where the failure was detected; zero for a matching limit.

Pattern#

silk
pub struct Pattern

An 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#

silk
pub struct Regex

The owner of native ECMAScript Unicode pattern validation and unanchored matching operations.

Implementation Regex#

silk
impl Regex

Associated function compile#

silk
pub effect fn compile(pattern: string) -> Pattern ! RegexError | OutOfMemoryError ? &mut Allocator

Compiles 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#

  • RegexError for invalid syntax, more than 65,536 pattern bytes, group nesting beyond 256, or a repetition bound greater than 1,000,000.

  • OutOfMemoryError if the allocator cannot supply the program.

Associated function validate#

silk
pub effect fn validate(pattern: string) -> () ! RegexError | OutOfMemoryError ? &mut Allocator

Checks 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#

  • RegexError for invalid syntax, more than 65,536 pattern bytes, nesting beyond 256 groups, or a repetition bound greater than 1,000,000.

  • OutOfMemoryError if the allocator cannot supply the parsed pattern.

Associated function matches#

silk
pub effect fn matches(pattern: string, text: string) -> bool
    ! RegexError | OutOfMemoryError ? &mut Allocator

Returns 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#

  • RegexError for 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 as false.

  • OutOfMemoryError if the allocator cannot supply the program or matcher state.

Associated function test#

silk
pub effect fn test(pattern: &Pattern, text: string) -> bool
    ! RegexError | OutOfMemoryError ? &mut Allocator

Searches 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#

  • RegexError with MatchLimit after 1,000,000 matcher steps or 1,024 recursive frames. Exhaustion is never reported as false.

  • OutOfMemoryError if 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.

View the source of this page