Help for this page

Select Code to Download


  1. or download this
     
    $_ = '       blee blah    ';
    s/\s*(.*?)\s*/$1/;
    print "matched '$&'; retained '$1'; str is '$_'\n";
    
  2. or download this
    matched '       '; retained ''; str is 'blee blah    '
  3. or download this
    s/^\s+//; 
    s/\s+$//;
    
  4. or download this
    [matt@blade8 matt]$ perl -MBenchmark -e '@list1 = @list2 = map { " "x 
    +rand(100) . "some text" . " "x rand(100)  } 1 .. 100; timethese( 100_
    +000, { single_regexp => sub { return map {  s/^\s*(.+?)\s*$/$1/; $_ }
    + @list1 }, dual_regexp => sub { return map { s/^\s+//; s/\s+$//; $_ }
    + @list2 }  } ); '
    Benchmark: timing 100000 iterations of single_regexp, dual_regexp...
       single_regexp: 56 wallclock secs (56.04 usr +  0.00 sys = 56.04 CPU
    +) @ 1784.44/s (n=100000)
       dual_regexp: 17 wallclock secs (18.63 usr +  0.00 sys = 18.63 CPU) 
    +@ 5367.69/s (n=100000)