in reply to Re: •Re: positions of all regexp matches
in thread positions of all regexp matches

() = $string =~ /(?=$regex)(?{push @matches, pos})./g;
Oddly enough, that would find no matches if $regex began with \n. Sloppy coding. No need for that "." in there anyway.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re:^4 positions of all regexp matches
by flounder99 (Friar) on Oct 14, 2003 at 19:57 UTC
    You are right about the not finding the \n since I didn't add an s at the end of the m//. But if you don't have the . in there it will find the positions twice.
    use re 'eval'; my $string = "he\nllo world ba\nllloon"; my $regex = qr/\nll/; my @matches; () = $string =~ /(?=$regex)(?{push @matches, pos})./sg; print "with .\n", join "\n", @matches; @matches = (); () = $string =~ /(?=$regex)(?{push @matches, pos})/sg; print "\n\nwithout .\n", join "\n", @matches; __END__ with . 2 15 without . 2 2 15 15

    --

    flounder

        Maybe it's something to do with the RE engine trying some optimization.

        The RE engine can execute a code block more than once in a single pattern match attempt, and this would lead to multiple entries for a given pos.