Skip to content

Core\Regex\Match::group

$match->group(int|string $group): ?string$match->group(
    int|string $group,
): ?string

Returns the text of one group of a match, by the group's number or by its name.

A group is a part of the pattern in round brackets. Groups are numbered from 1, left to right, and group 0 is the whole match. A group written as (?<name>...) also has a name, and you can read it by that name.

The result is null when the pattern has the group, but this match did not use it. This happens with an optional group, such as (\d+)?. If the pattern has no group with that number or name, group throws a RuntimeError. So a typing mistake in a group name is an error, and not an empty value.

The examples show reading groups by number, a group that did not match, and reading the parts of a log line by name.

Parameters

  • $groupint|stringThe number of the group, or its name. Group 0 is the whole match.

Return value

Returns ?string. The text of the group. The result is null if the pattern has the group but this match did not use it.

Errors

  • RuntimeErrorThe pattern has no group with this number or name.

Examples

Read groups by number

<?nvs
// Shows how to read the groups of a match by their numbers.
// The pattern has two groups: the product code and the size.
var $m = Core\Regex::match('Item: SHIRT-42 (L)', '([A-Z]+-\d+) \((\w+)\)');
if ($m != null) {
// `group(0)` is the whole match. Group 1 and group 2 are the two parts in brackets.
echo 'whole: ', $m->group(0), "\n";
echo 'code: ', $m->group(1), "\n";
echo 'size: ', $m->group(2), "\n";
}
Output
whole: SHIRT-42 (L)
code: SHIRT-42
size: L

A group that did not match

<?nvs
// Shows the two cases where `group` has no text: an optional group, and a group that does not exist.
// `(\.\d+)?` is optional. In "42" it does not match, so group 2 is `null`.
array<string> $prices = ['42.50', '42'];
foreach ($prices as string $price) {
var $m = Core\Regex::match($price, '^(\d+)(\.\d+)?$');
if ($m != null) {
echo $price, ': whole part ', $m->group(1), ', fraction ', $m->group(2) ?? 'none', "\n";
}
}
// The pattern has only two groups, so asking for group 3 throws an error.
var $short = Core\Regex::match('42', '^(\d+)(\.\d+)?$');
if ($short != null) {
try {
echo $short->group(3) ?? 'null', "\n";
} catch (RuntimeError $e) {
echo 'error: ', $e->message, "\n";
}
}
Output
42.50: whole part 42, fraction .50
42: whole part 42, fraction none
error: Core\Regex\Match::group(): the pattern declares no group `3`

Read a log line by name

<?nvs
// Shows how to read the parts of a web server log line by their names.
// Each `(?<name>...)` is a group with a name. `group("status")` reads it by that name.
Core\Regex\Pattern $line = Core\Regex::compile('^(?<method>[A-Z]+) (?<path>\S+) (?<status>\d{3})$');
array<string> $log = ['GET /orders/42 200', 'POST /cart 500', 'not a request'];
foreach ($log as string $entry) {
var $m = Core\Regex::match($entry, $line);
if ($m == null) {
echo 'skipped: ', $entry, "\n";
} else {
echo $m->group('status'), ' for ', $m->group('method'), ' ', $m->group('path'), "\n";
}
}
Output
200 for GET /orders/42
500 for POST /cart
skipped: not a request