Skip to content

pcre: describe PREG_UNMATCHED_AS_NULL for trailing groups - #5272

Merged
lacatoire merged 1 commit into
php:masterfrom
lacatoire:fix/preg-unmatched-as-null-description
Aug 21, 2026
Merged

lacatoire merged 1 commit into
php:masterfrom
lacatoire:fix/preg-unmatched-as-null-description

Conversation

@lacatoire

@lacatoire lacatoire commented Feb 5, 2026 •

Copy link
Copy Markdown
Member

PREG_UNMATCHED_AS_NULL is described as:

If this flag is passed, unmatched subpatterns are reported as null; otherwise they are reported as an empty string.

That is incomplete for preg_match(): without the flag, an unmatched subpattern is only reported as an empty string when a later subpattern matched. Trailing unmatched subpatterns are dropped from $matches entirely.

preg_match('/(a)(b)?(c)?/', 'a', $m);                            // array(2): 0, 1
preg_match('/(a)(b)?(c)?/', 'a', $m, PREG_UNMATCHED_AS_NULL);    // array(4): 2 and 3 are null

Measured behaviour of the second call:

Version Result
7.2.34, 7.3.33 [0 => "a", 1 => "a"]
7.4.33 to 8.5.8 [0 => "a", 1 => "a", 2 => null, 3 => null]

Hence the 7.4.0 changelog entry, which follows UPGRADING for PHP 7.4:

When PREG_UNMATCHED_AS_NULL mode is used, trailing unmatched capturing groups will now also be set to null (or [null, -1] if offset capture is enabled). This means that the size of the $matches will always be the same.

The [null, -1] form is confirmed too: on 7.4.33 and 8.5.8, PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE yields 2 => [null, -1], while 7.3.33 omits the entry.

preg_match_all() is deliberately left untouched: it never had this behaviour. In PREG_PATTERN_ORDER the array is indexed by group, so trailing groups are always present — as empty strings without the flag and as null with it, on 7.2.34 as on 8.5.8. Nothing changed there in 7.4.

Fixes: #3483

@lacatoire lacatoire changed the title Clarify PREG_UNMATCHED_AS_NULL behavior for trailing groups Update PREG_UNMATCHED_AS_NULL description for trailing groups Mar 2, 2026
@lacatoire lacatoire closed this Mar 2, 2026
@lacatoire lacatoire reopened this May 13, 2026
@lacatoire
lacatoire force-pushed the fix/preg-unmatched-as-null-description branch from 80b0ee0 to 339dc08 Compare June 23, 2026 14:11
@lacatoire
lacatoire force-pushed the fix/preg-unmatched-as-null-description branch from 339dc08 to c8f311d Compare August 21, 2026 08:49
@lacatoire lacatoire changed the title Update PREG_UNMATCHED_AS_NULL description for trailing groups pcre: describe PREG_UNMATCHED_AS_NULL for trailing groups Aug 21, 2026
@lacatoire
lacatoire merged commit cdbe6f9 into php:master Aug 21, 2026
2 checks passed
@lacatoire
lacatoire deleted the fix/preg-unmatched-as-null-description branch August 21, 2026 08:52
KentarouTakeda added a commit to php/doc-ja that referenced this pull request Oct 4, 2026
php/doc-en@aa5a948a11 (php/doc-en#5652)、php/doc-en@cdbe6f9826 (php/doc-en#5272)、
php/doc-en@f88f34ad2f (php/doc-en#5887) に追従します。

PREG_UNMATCHED_AS_NULL の説明に、末尾のマッチしなかったサブパターンの扱い
(フラグなしでは結果に含まれず、フラグありでは &null; として含まれる) を追加し、
その例と出力、7.4.0 の変更履歴を加えました。

offset の注記の文言は php/doc-en@aa5a948a11 で変更され、php/doc-en@f88f34ad2f で
元に戻されているため、該当文はタグの変更のみです。

## 対象ファイル (1件)

- reference/pcre/functions/preg-match.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PREG_UNMATCHED_AS_NULL description

1 participant