in reply to Regex: continue from previous match

But I don't want to match only at that position, like the \G anchor would have.

Then don't use \G. Without \G the construct does exactly what you want:

use 5.010; $_ = 'aaababaab'; while (/(a+)/pcg) { say pos($_), " ", $1; } __END__ 3 aaa 5 a 8 aa

Replies are listed 'Best First'.
Re^2: Regex: continue from previous match
by John M. Dlugosz (Monsignor) on Apr 23, 2011 at 08:13 UTC
    Hmm, Why is it behaving differently?! Ahh, I think my problem concerns my use of PREMATCH, not the matching itself.

    PREMATCH is giving me the whole beginning of the string to the next match, not the part between the last search result and the next.

    use 5.10.1; use utf8; my $simples = qr{\*\*|//}; my $ps= qr/ (?<simple>$simples)(?<body>.*?)\k<simple> /x; my @results; my $line= q[You can make things **bold** or //italic// or **//both//** + or //**both**//.]; say "Original: [$line]"; while ($line =~ /$ps/pgc) { say "pos is " . pos($line); say "PREMATCH: (${^PREMATCH})" unless length(${^PREMATCH}) == 0; say "SIMPLE: ($+{simple}) ($+{body})"; }
    I guess to find the stuff between matches as well as the matches, I need to put it explicitly within a capture too. A lazy match of anything first.

    Thanks.

      I guess to find the stuff between matches as well as the matches, I need to put it explicitly within a capture too.

      Either do \G(.*?)$your_regex_here and have the stuff between matches in $1, or you could just store the previous value of pos + length(match), and then get the stuff between matches with substr.