API Reference › Regex
namespace

Regex

Regular expressions with PCRE/PECL-compatible syntax over a **linear-time** Thompson NFA (a Pike VM), so a bad pattern from a modder can never trigger catastrophic backtracking — matching is O(n·m), never exponential. Compile a pattern once and reuse it; an invalid pattern is an error value, never a crash. Supported: literals, ., classes [...] with ranges/negation and \d \w \s (and their negations), anchors ^ $, alternation |, capturing and (?:…) groups, and the quantifiers * + ? and {n,m} in greedy or lazy form. Backreferences and look-around are out of scope for a linear engine.

Regex.compile(pattern) -> RegexCompile a pattern once for reuse; returns an error value (null) if it is invalid.Regex.valid(pattern) -> boolTrue if the pattern is well-formed (compiles without error).Regex.matches(text, pattern) -> boolTrue if the pattern matches anywhere in the text (a search).Regex.test(text, re) -> boolLike matches, but reuses an already-compiled Regex.Regex.find(text, pattern) -> MatchThe first match of the pattern in the text (a Match, or null if none).Regex.exec(text, re) -> MatchLike find, but with an already-compiled Regex.Regex.next(text, re, from) -> MatchThe next match at or after byte offset from — the basis of a find-all loop.Regex.replace(text, pattern, replacement) -> stringReplace every match; the replacement expands \0..\9 group references.Regex.group(match, n) -> stringThe text of capture group n (group 0 is the whole match); empty if unset.Regex.group_count(match) -> intHow many capture groups the match's pattern has.Regex.start(match, n) -> intThe start byte offset of group n, or -1 if it did not participate.Regex.end(match, n) -> intThe end byte offset of group n, or -1 if it did not participate.Regex.ok(match) -> boolTrue if the match succeeded (the Match is non-null).
← All symbols