# preg_match() option for anchored offset?

- Thread: https://externals.io/message/99442
- Original archive: https://news-web.php.net/php.internals/99442
- Started by Rasmus Schultz on 2017-06-07T20:03:31+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #99442 — Rasmus Schultz — 2017-06-07T20:03:31+00:00

URL: https://externals.io/message/99442#99442

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?

---

## #99443 — Rowan Collins — 2017-06-07T20:13:17+00:00

URL: https://externals.io/message/99442#99443 · In reply to: #99442

> 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?
>

How would you propose to implement this? Is it something PCRE already 
supports? Or would you manipulate the subject string pointer at the 
point it's passed down? The latter approach seems doable, but I'm not 
sure if there are any subtle gotchas.

Regards,

-- 
Rowan Collins
[IMSoP]

---

## #99444 — Nikita Popov — 2017-06-07T20:13:34+00:00

URL: https://externals.io/message/99442#99444 · In reply to: #99442

> 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

---

## #99450 — Rasmus Schultz — 2017-06-08T07:10:33+00:00

URL: https://externals.io/message/99442#99450 · In reply to: #99444

> 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:03 PM, Rasmus Schultz <rasmus@mindplay.dk>
> wrote:
>
> > […]
>
> You are looking for the \G anchor or the A modifier.
>
> Nikita
