Core\Regex
Finds, extracts, replaces and splits text with regular expressions. Every method takes a pattern as a string or as a Core\Regex\Pattern that compile returns.
A Core\Regex pattern is a plain string — no /…/ delimiters and no trailing modifiers; the flags are options to Core\Regex::compile, which answers a Core\Regex\Pattern every other member also takes in place of the string. A single-quoted string is the cheap spelling — '\d+' has no escapes to double, where "\\d+" does. A pattern runs on a linear-time engine; one that needs a lookaround or a backreference runs on a backtracking engine under a step budget, and exhausting that budget throws rather than answering false. A pattern neither engine can compile throws too. match answers ?Core\Regex\Match — null when nothing matched — and matchAll a list of them; in replace's template $1 and ${name} are group references and $$ a plain $, while the value replaceWith's callback returns is inserted as it is. Fixed text built into a pattern goes through quote.
<?nvsstring $log = '2026-08-30 GET /a 200; 2026-08-31 POST /b 404';echo Core\Regex::matches($log, "\\b\\d{3}\\b") ? 'has status' : 'none', "\n";var $m = Core\Regex::match($log, "(?<y>\\d{4})-(?<mo>\\d{2})-(?<d>\\d{2})");echo $m?->text() ?? 'none', ' at ', $m?->offset() ?? -1, ' day=', $m?->group('d') ?? '?', "\n";foreach (Core\Regex::matchAll($log, "(GET|POST) (\\S+)") as Core\Regex\Match $hit) { echo $hit->group(1) ?? '?', ' ', $hit->group(2) ?? '?', "\n";}echo Core\Regex::replace('2026-08-30', "(\\d+)-(\\d+)-(\\d+)", '$3/$2/$1'), "\n";echo Core\Regex::replaceWith('a1b22', "\\d+", fn(Core\Regex\Match $x): string => '<' . $x->text() . '>'), "\n";echo Core\Str::join(Core\Regex::split('a, b,c', ",\\s*"), '|'), "\n";Core\Regex\Pattern $word = Core\Regex::compile('^[a-z]+$', { caseInsensitive: true });echo Core\Regex::matches('Hello', $word) ? 'word' : 'not a word', "\n";echo Core\Regex::quote('a.b*c?'), "\n";has status2026-08-30 at 0 day=30GET /aPOST /b30/08/2026a<1>b<22>a|b|cworda\.b\*c\?Members
8 members.
| Member | Signature |
|---|---|
compile | compile(string $pattern, { caseInsensitive?: bool, multiline?: bool, dotAll?: bool, ungreedy?: bool }): Core\Regex\Pattern |
matches | matches(string $subject, Core\Regex\Pattern|string $pattern): bool |
match | match(string $subject, Core\Regex\Pattern|string $pattern, { from?: int }): ?Core\Regex\Match |
matchAll | matchAll(string $subject, Core\Regex\Pattern|string $pattern): array<Core\Regex\Match> |
replace | replace(string $subject, Core\Regex\Pattern|string $pattern, string $replacement, { limit?: uint }): string |
replaceWith | replaceWith(string $subject, Core\Regex\Pattern|string $pattern, callable(Core\Regex\Match): string $fn, { limit?: uint }): string |
split | split(string $subject, Core\Regex\Pattern|string $pattern, { limit?: int, keepEmpty?: bool }): array<string> |
quote | quote(string $text): string |
