in reply to Counting multiple matches of RegExp in single line
$count += () = $teststring =~ /(test)/g;
This syntax may be a little crypric at first, so here's the breakdown:
You can assign the results of a match to an array, like so: @matches = $teststring =~ /(test)/g;
Note the use of a capturing match (we use (test) in parentheses), and the /g modifier to match more than once.
Now if you have @matches, then using it in scalar context gives you how many matches there were, so this would work:
$count += @matches; The method I'm suggesting simply does this without the temporay variable. You do need to force the match to take place in list context, otherwise it'll just return 1 on successful matches; that's what the () = bit achieves.
|
|---|