in reply to Re: how to count the number of repeats in a string (really!)
in thread how to count the number of repeats in a string (really!)

I guess I didn't explain why mine works and why the OP's doesn't work with ".+". Compare

while ($str =~ /($re)/g) { print("$1\n"); }
use re 'eval'; $str =~ / ($re) (?{ print("$1\n"); }) (?!) /x;

Looks similar? But for the same input, they produce different results.

Input:

my $str = 'aabcdabcabce'; my $re = qr/a[^a]*/;

Output from /.../g:

a abcd abc abce

Output from /...(?{ save results })(?!)/:

a abcd abc ab a abc ab a abce abc ab a

/...(?{ save results })(?!)/ is key in finding all possible matches. It forces the regex to try everything to obtain a match.

Replies are listed 'Best First'.
Re^3: how to count the number of repeats in a string (really!)
by blazar (Canon) on Nov 14, 2007 at 21:49 UTC
    I guess I didn't explain why mine works and why the OP's doesn't work with ".+".

    I personally believe that blokhead already did. It was just a trivial mistake/overview on my part.