In plain, old, vanilla PHP there's no difference between:
- an invalid group (name
- valid, but non-existent group
- existent, but not matched group
- matched group, but value is
The two first cases always return
null, the third one returns either
null (depending on the order of
groups!). If you used
PREG_OFFSET_CAPTURE, it'll return
['', -1] instead (so you need to compare the offset to
Matched empty string, of course, returns
'' (which might the same as the third).
Since PHP 7.2, there's
PREG_UNMATCHED_AS_NULL - it's a little better, it allows distinguishing an unmatched subject
from a matched empty string, but to distinguish invalid and non-existent groups from unmatched - you have to use
And T-Regx hates it. So we fixed it all.
That's why in T-Regx,
Match details has 3 separate methods to deal with each of these cases separately.
Of course, the interface of
Match is the same for matching, replacing and any other operation (unlike vanilla-PHP),
so validation of groups in T-Regx works completely alike for
pattern()->replace() and any other
Match always has the same interface and works exactly alike, no matter where it was used.
Here's how they work:
|Not matched group|
|Matched group||Value of the group|
- You're protected from using an invalid group (
- You're protected from using a non-existent method (except with
- You're protected from using a non-matched group (except with