One issue is that the result is clouded by an extra bogus match. Only one of the 'tail' matches is required. Adding the second one is really bad news for the single regex variant. Choosing to 'nibble' (\s) instead of swallowing whole (\s+) does the single regex a huge disservice too. Consider the following benchmark:

Rate sExNibble singleNibble mExNibble multi multiNib +ble single sExNibble 1795/s -- -74% -99% -99% - +99% -99% singleNibble 6881/s 283% -- -95% -95% - +97% -98% mExNibble 134665/s 7403% 1857% -- -5% - +49% -60% multi 142315/s 7830% 1968% 6% -- - +46% -58% multiNibble 263146/s 14562% 3724% 95% 85% + -- -22% single 338640/s 18769% 4822% 151% 138% +29% --
use strict; use warnings; use Benchmark qw(cmpthese); my $testStr = (' ' x 1000) . 'x' . (' ' x 1000); cmpthese ( -1, { sExNibble => "sExNibbleRegex ('$testStr');", singleNibble => "singleNibbleRegex ('$testStr');", single => "singleRegex ('$testStr');", mExNibble => "mExNibbleRegex ('$testStr');", multiNibble => "multiNibbleRegex ('$testStr');", multi => "multiRegex ('$testStr');", } ); sub sExNibbleRegex { my ($string) = @_; $string =~ s/^\s|\s$|\s+$//g; } sub singleNibbleRegex { my ($string) = @_; $string =~ s/^\s|\s$//g; } sub singleRegex { my ($string) = @_; $string =~ s/^\s+|\s+$//g; } sub multiNibbleRegex { my ($string) = @_; $string =~ s/\^s//g; $string =~ s/\s$//g; } sub mExNibbleRegex { my ($string) = @_; $string =~ s/\^s//g; $string =~ s/\s$//g; $string =~ s/\s+$//g; } sub multiRegex { my ($string) = @_; $string =~ s/\^s+//g; $string =~ s/\s+$//g; }

Perl reduces RSI - it saves typing

In reply to Re: question about reg exp engine by GrandFather
in thread question about reg exp engine by chuckd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.