R
Rainer Weikusat
Charles DeRykus said:[...]Consider a string like "foo bar baz ". For /\S+\s*$/ perl tries the
following sequence of matches:
I thought a possessive quantifier would help with this more intuitive
alternative: (\S+)\s*$ -> (\S++)\s*+$. But, unless there's some basic
error on my part, then the possessive replacement ate the proverbial
dust even of the backtracking regex.
BTW:
-----------
use Benchmark qw(cmpthese);
use constant MAX_WORDS => 15;
use constant MAX_LEN => 20;
use constant LINES => 100;
my (@lines, $line, $n, $nw, $x);
srand(0xdeafbabe); # repeatable
$n = LINES;
do {
$nw = int(rand(MAX_WORDS - 2)) + 2;
$line = '';
do {
$line .= 'x' x (int(rand(MAX_LEN - 1)) + 1);
$line .= ' ' x (rand(5) + 1) if $nw > 1;
} while --$nw;
push(@lines, $line);
# print STDERR ("'$line'\n");
} while --$n;
cmpthese(-3,
{
split => sub {
$x = (split(/\s+/, $_))[-1] for @lines;
},
renb => sub {
/.*\s(\S+)$/ and $x = $1 for @lines;
},
rere => sub {
reverse($_) =~ /^(\S+)/ and $x = reverse($1) for @lines;
}});