Req #77459 [Asn]: Add partial match support to preg_match
| From: | nikic@php.net | Date: | Wed, 20 Mar 2019 09:24:04 +0000 |
| Subject: | Req #77459 [Asn]: Add partial match support to preg_match | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-220093@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77459&edit=1
ID: 77459
Updated by: nikic@php.net
Reported by: paul dot crovella at gmail dot com
Summary: Add partial match support to preg_match
Status: Assigned
Type: Feature/Change Request
Package: PCRE related
PHP Version: Next Minor Version
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
PR: https://github.com/php/php-src/pull/3969
Previous Comments:
------------------------------------------------------------------------
[2019-01-15 08:53:17] paul dot crovella at gmail dot com
Description:
------------
Please add support for PCRE2 partial matching[1] to preg_match.
A partial match is one that occurs when a subject matches a regex as far as it goes but ends before
a full match, or sure failure to match, can be determined. The regex /abc/ would normally fail to
match 'ab', but with one of the partial flags given (see below) it'd instead return a
partial match. This is useful to indicate that more data needs to be read into a buffer before
trying the match again. (See: multi-segment matching[2].)
I imagine this would come about as PREG_PARTIAL_SOFT and PREG_PARTIAL_HARD flags (neither on by
default) corresponding to PCRE2_PARTIAL_SOFT and PCRE2_PARTIAL_HARD, respectively, which would pass
through as options to pcre2_match and pcre2_jit_match. pcre2_jit_compile would get
PCRE2_JIT_PARTIAL_SOFT or PCRE2_JIT_PARTIAL_HARD as appropriate in addition to the usual
PCRE2_JIT_COMPLETE.
When a partial match is found preg_match should return a new value (2?) in order to distinguish it,
and the $matches array (if given) should be filled with a single member consisting of the string
participating in partial match (and possibly a little more.) This should be from the beginning of
the matched data minus the length of the longest lookbehind* through to the end of the subject
string. For example:
/baz(?<=barbaz)(?=quux)/
applied to:
abcfoobarbazqu
should result in foobarbazqu in the $matches array. This ensures enough of the string is kept for
further match attempts to have a chance to succeed. (Fortunately PCRE ignores \K in a partial match,
so there's no need to worry about it.) Another option that comes to mind would be to just give
the offset of this substring and let the user chop it off or pass it as the offset argument of
preg_match as desired.
---
* Available from pcre2_pattern_info given PCRE2_INFO_MAXLOOKBEHIND. See [3] for details.
[1] https://www.pcre.org/current/doc/html/pcre2partial.html
[2] https://www.pcre.org/current/doc/html/pcre2partial.html#SEC7
[3] https://www.pcre.org/current/doc/html/pcre2partial.html#SEC8
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77459&edit=1