Core\Regex::match
Core\Regex::match(
string $subject,
Core\Regex\Pattern|string $pattern,
{from?: int},
): ?Core\Regex\MatchFinds the first place where a pattern matches a text, and returns the match with its parts.
The result is a Core\Regex\Match, or null if the pattern matches nowhere. A Match has the matched text, the position where it starts, and the text of each group in the pattern. A group is a part of the pattern in round brackets, and you can read it by its number or by its name. The from option sets the position where the search starts. Positions count characters, not bytes.
The pattern must be text from your program. A pattern from a user does not compile here. To search for text a user typed, put it in the pattern with Core\Regex::quote.
Good to know: always check for null before you read the match. To find every match instead of the first one, use Core\Regex::matchAll. To only check whether the pattern matches, use Core\Regex::matches.
Parameters
$subjectstringThe text to search.$patternCore\Regex\Pattern|stringACore\Regex\PatternfromCore\Regex::compile, or a pattern string with no options. Ataintedstring does not compile here.
Options
fromintoptionalThe character position where the search starts. The default is0. A negative position counts from the end of$subject.
Return value
Returns ?Core\Regex\Match. The first Core\Regex\Match at or after from, or null if there is no match.
Errors
RuntimeError$patternis not a valid regular expression, or matching it against$subjectneeds more steps than the limit allows.
Examples
Groups
<?nvs// Shows how to read the parts of a text that the groups of a pattern match.
// `match` returns a `Match`, or `null` if the pattern does not match.// `group(1)` is the text the first `( )` matched, and `text()` is the whole match.var $m = Core\Regex::match('order-4711-eu', "order-(\\d+)-(\\w+)");if ($m != null) { echo 'id=', $m->group(1), "\n"; echo 'region=', $m->group(2), "\n"; echo 'whole=', $m->text(), "\n";}id=4711region=euwhole=order-4711-euSearch again after the first match
<?nvs// Shows how to find the next match by starting the search after the previous one.
string $text = 'Call 555-0142 or 555-0199.';
// The first search starts at position 0 and finds the first number.var $first = Core\Regex::match($text, '\d{3}-\d{4}');if ($first != null) { echo $first->text(), ' starts at ', $first->offset(), "\n";
// `from` starts the second search where the first match ends. // This prints "555-0199 starts at 17". int $after = $first->offset() + (Core\Str::length($first->text()) as int); var $second = Core\Regex::match($text, '\d{3}-\d{4}', { from: $after }); echo $second?->text() ?? 'none', ' starts at ', $second?->offset() ?? 0, "\n";}555-0142 starts at 5555-0199 starts at 17Read a date with named groups
<?nvs// Shows how to read the year, month and day from a date with named groups.
// `(?<year>...)` gives a group a name. `group("year")` reads it by that name.Core\Regex\Pattern $date = Core\Regex::compile('^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$');
array<string> $inputs = ['2026-03-14', '14.03.2026'];// The first date matches. The second one has another format, so `match` returns `null`.foreach ($inputs as string $input) { var $parts = Core\Regex::match($input, $date); if ($parts == null) { echo $input, ": not a date in the format YYYY-MM-DD\n"; } else { echo $input, ': day ', $parts->group('day'), ', month ', $parts->group('month'), ', year ', $parts->group('year'), "\n"; }}2026-03-14: day 14, month 03, year 202614.03.2026: not a date in the format YYYY-MM-DD