[php-src] Issue #11397: PCRE: Unmatched capture groups result inconsistency
| From: | mvorisek | Date: | Wed, 07 Jun 2023 21:37:09 +0000 |
| Subject: | [php-src] Issue #11397: PCRE: Unmatched capture groups result inconsistency | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244619@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11397
Author: mvorisek
### Description
Currently, unmatched capture groups are added to the result when they are before matched capture
group, but are not added when they are after (ie. unmatched capture group is not followed by any
other matched capture group).
<br>
The docs about "PREG_UNMATCHED_AS_NULL" flag says:
> If this flag is passed, unmatched subpatterns are reported as null; otherwise they are reported
> as an empty string.
So **all** capture groups, even the unmatched at the end, should be always present.
<br>
The following code:
```php
<?php
preg_match('/(?<foo>y)?x(?<bar>y)?/', 'x', $matches);
print_r($matches);
preg_match('/(?<foo>y)?()x(?<bar>y)?/', 'x', $matches); // notice
extra "()"
print_r($matches);
```
Resulted in this output:
```
Array
(
[0] => x
)
Array
(
[0] => x
[foo] =>
[1] =>
[2] =>
)
```
But I expected this output instead:
```
Array
(
[0] => x
[foo] =>
[1] =>
[2] =>
[bar] =>
[3] =>
)
Array
(
[0] => x
[foo] =>
[1] =>
[2] =>
[bar] =>
[3] =>
)
```
online repro: https://3v4l.org/ra13Q