Re: preg_match() option for anchored offset?
| From: | Rasmus Schultz | Date: | Thu, 08 Jun 2017 07:10:33 +0000 |
| Subject: | Re: preg_match() option for anchored offset? | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-99450@lists.php.net to get a copy of this message | ||
> You are looking for the \G anchor or the A modifier.
Both of these options work great!
I've submitted a patch to the manual page with a note explaining these
options.
Thanks :-)
On Wed, Jun 7, 2017 at 10:13 PM, Nikita Popov <nikita.ppv@gmail.com> wrote:
> On Wed, Jun 7, 2017 at 10:03 PM, Rasmus Schultz <rasmus@mindplay.dk>
> wrote:
>
>> What do you think about adding another option to preg_match() to allow the
>> $offset parameter to be treated as the start anchor?
>>
>> The manual proposes to do this:
>>
>> $subject = "abcdef";
>> $pattern = '/^def/';
>> $offset = 3;
>> preg_match($pattern, substr($subject, $offset), $matches);
>>
>> In other words, use substr() to copy the entire remainder of the string.
>>
>> I just wrote a simple SQL parser tonight, and had to use this approach,
>> which (I imagine) must be pretty inefficient?
>>
>> I'd like to be able to do the following:
>>
>> $subject = "abcdef";
>> $pattern = '/^def/';
>> $offset = 3;
>> preg_match($pattern, $subject, $matches, PREG_ANCHOR_OFFSET, $offset);
>>
>> This new option would make the ^ anchor work from the given $offset, which
>> allows me to parse the entire $subject without copying anything.
>>
>> Thoughts?
>>
>
> You are looking for the \G anchor or the A modifier.
>
> Nikita
>
>