Skip to content

Core\Regex::match

Finds 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|stringA Core\Regex\Pattern from Core\Regex::compile, or a pattern string with no options. A tainted string does not compile here.

Options

  • fromintoptionalThe character position where the search starts. The default is 0. 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$pattern is not a valid regular expression, or matching it against $subject needs 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";
}
Output
id=4711
region=eu
whole=order-4711-eu

Search 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";
}
Output
555-0142 starts at 5
555-0199 starts at 17

Read 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";
}
}
Output
2026-03-14: day 14, month 03, year 2026
14.03.2026: not a date in the format YYYY-MM-DD