in reply to regex: list each ocurrence of a pattern in a line

Meet the "g" modifier.

my @matches; for (@fields) { push @matches, /%%(\w+?)%%/g; }
Or even
my @matches = map /%%(\w+?)%%/g, @fields;

The "?" quantifier isn't necessary. I don't know if it's faster with or without it. If probably only matters if the elements of @fields are very long strings, so you might as well leave it out.

Replies are listed 'Best First'.
Re^2: regex: list each ocurrence of a pattern in a line
by spatterson (Pilgrim) on Nov 19, 2008 at 12:05 UTC
    Thanks, thats just what I needed, and a use for map too.

    just another cpan module author