in reply to Re: Regex: continue from previous match
in thread Regex: continue from previous match

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.

Replies are listed 'Best First'.
Re^3: Regex: continue from previous match
by moritz (Cardinal) on Apr 23, 2011 at 08:39 UTC
    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.