use Test::More;
sub trim { my $s = $_[0]; $s =~ s/^\s+(\S?.*\S)\s+$/$1/s; $s }
is( trim(' '), '' );
is( trim('a '), 'a' );
is( trim(' a'), 'a' );
is( trim(' a '), 'a' );
is( trim('ab '), 'ab' );
is( trim(' ab'), 'ab' );
is( trim(' ab '), 'ab' );
is( trim('a bb c '), 'a bb c' );
is( trim(' a bb c'), 'a bb c' );
is( trim(' a bb c '), 'a bb c' );
done_testing();
__END__
not ok 1
# Failed test at ./t.pl line 12.
# got: ' '
# expected: ''
not ok 2
# Failed test at ./t.pl line 13.
# got: 'a '
# expected: 'a'
not ok 3
# Failed test at ./t.pl line 14.
# got: ' a'
# expected: 'a'
ok 4
not ok 5
# Failed test at ./t.pl line 16.
# got: 'ab '
# expected: 'ab'
not ok 6
# Failed test at ./t.pl line 17.
# got: ' ab'
# expected: 'ab'
ok 7
not ok 8
# Failed test at ./t.pl line 19.
# got: 'a bb c '
# expected: 'a bb c'
not ok 9
# Failed test at ./t.pl line 20.
# got: ' a bb c'
# expected: 'a bb c'
ok 10
1..10
# Looks like you failed 7 tests of 10.
The one I could find with best benchmark and passes tests is s/^\s*((?:.*\S)?)\s*$/$1/s;, which is essentially like MRE_regx with + replaced with * (perhaps trizen typo-ed?)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|