in reply to Re^2: Get chars between 2 markers using regular expressions
in thread Get chars between 2 markers using regular expressions

You're only matching the regex once, not collecting all instances of the match. The difference gets more pronounced the longer the string becomes:

use strict; use warnings; use Benchmark qw(:all); my $string="He0Hello~~He2W~orld~~He0Hello~~He2W~orld~~He0Hello~~He2W~o +rld~~He0Hello~~He2W~orld~~"; my $f; sub invertedCharclass { while($string=~m/He\d([^~]+)~~/g){$f=$1} } sub nonGreedy { while($string=~m/He\d(.+?)~~/g){$f=$1} } cmpthese (-10, { '[^~]+' => \&invertedCharclass, '.+?' => \&nonGreedy, } );
gives this on my machine:
Rate .+? [^~]+ .+? 105088/s -- -28% [^~]+ 146676/s 40% --

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan