Here's a benchmark, using the mentioned single regex, the double regex from the FAQ (also mentioned), BrowserUKs solution, and the one from Regexp::Common:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark 'cmpthese';
use Regexp::Common;
use Test::More tests => 4;
our @data = (
" White space in front",
"White space at back ",
"Nowhitespaceatall",
"White space, but not in front or at back",
" White space in front, and at back "
);
our @expected = (
"White space in front",
"White space at back",
"Nowhitespaceatall",
"White space, but not in front or at back",
"White space in front, and at back"
);
our (@s, @d, @b, @r);
print '#';
cmpthese -1, {
'#single' => '@s = @data; s/^\s*(.*?)\s*$/$1/s for @s',
'#duo' => '@d = @data; s/^\s+//, s/\s+$// for @d',
'#browseruk' => '@b = @data; s/^\s*(.*)\b\s*$/$1/s for @b',
'#R::C' => '@r = map {$RE{ws}{crop}->subs($_)} @data',
};
is_deeply (\@s, \@expected, "single");
is_deeply (\@d, \@expected, "duo");
is_deeply (\@b, \@expected, "browseruk");
is_deeply (\@r, \@expected, "R::C");
__END__
1..4
# Rate #R::C #single #browseruk #duo
#R::C 1914/s -- -87% -94% -97%
#single 15175/s 693% -- -53% -75%
#browseruk 32288/s 1587% 113% -- -46%
#duo 59650/s 3017% 293% 85% --
ok 1 - single
ok 2 - duo
ok 3 - browseruk
ok 4 - R::C
| [reply] [d/l] |