Stripping whitespace from both ends of a string…

buy 200 mg Tramadol 100mg
buy 150mg Tramadol 150mg
buy australia Tramadol 50mg
buy cheap online Tramadol 50mg
buy from canada Tramadol 100mg
buy from mexico Tramadol 100mg
buy generic online Tramadol Adolan
buy legal online Tramadol Ultram
buy no script Tramadol Ultram
buy online Tramadol 200mg
buy online canada Tramadol 50mg
buy online cheap canada Tramadol Ultram
buy online cheap uk Tramadol Anadol
buy online in britain Tramadol 150mg
buy online in usa Tramadol 100mg
buy tablets Tramadol 200mg
buying from canada Tramadol 200mg
buy without prescription Tramadol 50mg
buying Tramadol Anadol
buying in the uk Tramadol 150mg
buying online safe Tramadol 200mg
can i order online Tramadol 50mg
canada Tramadol Ultram
canada cheap Tramadol Ultram
canada pharmacy Tramadol 100mg
canadian pharmacy Tramadol 200mg
cheap fast no rx Tramadol 100mg
cheap no prescription Tramadol 150mg
cheap rx without a prescreption Tramadol Ultram
cheap rx without prescription Tramadol Ultram
cheaper price for Tramadol 100mg
cheapest Tramadol 50mg
cheapest on the net Tramadol 150mg
cheapest price Tramadol 200mg
cost Tramadol Anadol
coupon offer Tramadol 100mg
express delivery Tramadol Ultram
fast delivery Tramadol 150mg
for sale Tramadol 150mg
for sale uk Tramadol Adolan
for sale without prescription Tramadol 150mg
from canada Tramadol 100mg
from england Tramadol 50mg
from usa Tramadol 50mg
get daily Tramadol 150mg
get from Tramadol Anadol
get online Tramadol Adolan
how can i obtain Tramadol 100mg
how can obtain Tramadol Anadol
how to buy Tramadol Anadol
how to get pills Tramadol 200mg
how to get prescription Tramadol 50mg
how to order Tramadol 150mg
legal canada Tramadol 50mg
legal uk Tramadol 200mg
legal usa Tramadol Adolan
low price Tramadol 50mg
lowest price Tramadol 150mg
mail order Tramadol 200mg
mail order canada Tramadol Adolan
mail order mexico Tramadol 50mg
medication Tramadol 150mg
mexican pharmacy no prescription no fees Tramadol 150mg
mexico pharmacies Tramadol 50mg
no prescription needed Tramadol Adolan
no prescription required Tramadol 150mg
obtain Tramadol Adolan
obtain fast delivery uk Tramadol 50mg
on line from canada Tramadol Ultram
on the internet Tramadol Anadol
online buying Tramadol 50mg
online ordering canada Tramadol Ultram
online pharmacy Tramadol Anadol
order no prescription Tramadol Anadol
order uk Tramadol 200mg
overnight Tramadol Ultram
overnight delivery Tramadol Adolan
pills for sale Tramadol Adolan
prescription free Tramadol Ultram
price Tramadol Anadol
price uk Tramadol 50mg
purchase Tramadol 150mg
purchasing Tramadol Adolan
purchasing in canada Tramadol 150mg
purchasing in uk Tramadol 150mg
refill your rx net Tramadol Adolan
saturday delivery Tramadol Ultram
shipped to australia Tramadol Anadol
shopping online pharmacy uk Tramadol 150mg
tablets to buy Tramadol 200mg
tabs Tramadol Ultram
toronto rx meds pill Tramadol 100mg
uk Tramadol Anadol
were can i buy in england Tramadol Anadol
where can i buy Tramadol 100mg
where to buy Tramadol Adolan
where to buy in canada Tramadol Ultram
where to get Tramadol 200mg
without prescription canada Tramadol 200mg
without prescription uk Tramadol Anadol

I was just in a room with three competent, professional Perl developers, all of whom agreed that you can’t strip whitespace from both ends of a string in a single regexp.

The common knowledge is:

$str =~ s/^\s+//;

$str =~ s/\s+$//;

I fiddled around and found this:

$str =~ s/^\s*(.*?)\s*$/$1/;

I think it’s right.  Is it better?  Not sure.   Is it faster?  Slower?  Clearer?

Speed I can test with benchmark… and using two is faster.

% perl try.pl
Rate single multi
single 444444/s — -74%
multi 1724138/s 288% —

So, nevermind.  Use two regexps.

Is one clearer?  Not sure.  But it’s slower!

Tags:

13 Responses to “Stripping whitespace from both ends of a string…”

  1. chackoc says:

    $str =~ s/(^\s+|\s+$)//g;

  2. brunov says:

    For the epitome of clear:

    use perl5i::latest;

    $str->trim;

  3. Adam Kennedy says:

    $str =~ s/(?:^\s+|\s+$)//g;

  4. zloyrusskiy says:

    I’m using:

    s/(?:^\s+|\s+$)//g

    it’s one regular expression + has good speed

  5. Mark Grimes says:

    What about:

    $str =~ s/^\s*|\s*$//g;

    I can’t test or benchmark it right now, but I know I’ve used something along these lines before.

  6. Yanick says:

    What about

    $str =~ s/^\s+|\s+$//g

    ?

  7. Yanick says:

    What about

    $str =~ s/^\s+|\s+$//g

    ?

    It’s faster, and not too obtuse.

  8. Dave Rolsky says:

    Yes, it’s slower, but look at the absolute speed. The “slow” version can still execute 440,000 or so per minute!

    Write code as clearly as possible, _then_ optimize the slow parts. This is what profilers are for.

    This sort of premature micro-optimization makes for messy code that may actually be no faster than clean code.

    Also, I think the simplest way to write it is …

    $str =~ s/^\s+|\s+$//g;

  9. Hercynium says:

    What about:

    $str =~ s/^\s*|\s*$//g

    Seems clearer and will probably be faster.

  10. mattp says:

    its slower due to the backtracking required from the combination of two greedy quantifiers with the one non greedy.
    to quote knuth .. premature optimization is the root of all evil

  11. dami says:

    Jeffrey Friedl recommends using 2 regexes. But you can do it in one line :

    s/^\s+//, s/\s+$// for $str;

  12. John Wiersba says:

    I think the hands down winner is $str =~ s/^\s+//; $str =~ s/\s+\z//;

    It says exactly what you want and it’s the fastest. No need to “optimize” with a single regex. Although dami’s postfix-for form is 7 characters shorter, the need for a comma instead of semicolon is a blemish, plus it’s about 25% slower.

    #!/usr/bin/perl -w
    use strict;
    use Benchmark;
    use vars qw($str1);
    $str1 = ” asf asd fasd fasd f asdf asd asdf as fsda fasdf “;
    timethese( -5, {
    test1_0 => ‘ $str = $str1; $str =~ s/^\s+|\s+\z//g ‘,
    test1_1 => ‘ $str = $str1; $str =~ s/^\s*|\s*\z//g ‘,
    test1_2 => ‘ $str = $str1; $str =~ s/^\s+|\s+$//g ‘,
    test1_3 => ‘ $str = $str1; $str =~ s/^\s*|\s*$//g ‘,
    test2_0 => ‘ $str = $str1; $str =~ s/^\s+//; $str =~ s/\s+\z// ‘,
    test2_1 => ‘ $str = $str1; $str =~ s/^\s+//; $str =~ s/\s+$// ‘,
    test2_2 => ‘ $str = $str1; $str =~ s/^\s*//; $str =~ s/\s*\z// ‘,
    test2_3 => ‘ $str = $str1; $str =~ s/^\s*//; $str =~ s/\s*$// ‘,
    test3 => ‘ $str = $str1; s/^\s+//, s/\s+\z// for $str ‘,
    });

    Benchmark: running test1_0, test1_1, test1_2, test1_3, test2_0, test2_1, test2_2, test2_3, test3 for at least 5 CPU seconds…
    test1_0: 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 185981.10/s (n=983840)
    test1_1: 5 wallclock secs ( 5.22 usr + 0.00 sys = 5.22 CPU) @ 154480.84/s (n=806390)
    test1_2: 6 wallclock secs ( 5.22 usr + 0.00 sys = 5.22 CPU) @ 188475.10/s (n=983840)
    test1_3: 5 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU) @ 154564.00/s (n=811461)
    test2_0: 5 wallclock secs ( 5.26 usr + 0.01 sys = 5.27 CPU) @ 672072.87/s (n=3541824)
    test2_1: 6 wallclock secs ( 5.44 usr + 0.00 sys = 5.44 CPU) @ 651070.77/s (n=3541825)
    test2_2: 5 wallclock secs ( 5.17 usr + 0.00 sys = 5.17 CPU) @ 248851.26/s (n=1286561)
    test2_3: 5 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 241523.71/s (n=1263169)
    test3: 4 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 526948.02/s (n=2787555)

  13. […] noticed a couple blog entries in the Planet Perl Iron Man feed discussing which way of stripping whitespace from […]

Leave a Reply