in reply to Regex: continue from previous match

Then don't use \G

while ('abxxxacxxxad' =~ /a(.)/g) { say $1; }

Replies are listed 'Best First'.
Re^2: Regex: continue from previous match
by John M. Dlugosz (Monsignor) on Apr 23, 2011 at 08:02 UTC
    That's what I have (no \G) and it doesn't work. Your example of an 'a' followed by any one char will not match variable length or different ways at the same position, so it's moot. Put two a's in a row in your test string and you'll see overlapping matches.

     perl -E'while ("abaaxxxacxxad" =~ /a(.)/g) { say $& }'

      Your example of an 'a' followed by any one char will not match variable length

      Sure it works with variable length patterns.

      while ('abxxxacdxxxae' =~ /a([^x]+)/g) { say $1; }

      Your example of an 'a' followed by any one char will not match [...] different ways at the same position

      Cause you didn't ask that. You asked about matching no earlier than pos.

      The only way to make the regex engine backtrack is to cause the match to fail.

      'abcd' =~ /(.*) (?{ say $1 }) (?!)/sx