Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Weird Regex in CGI::Cookie

by d_i_r_t_y (Monk)
on Oct 16, 2003 at 00:59 UTC ( #299601=note: print w/replies, xml ) Need Help??


in reply to Weird Regex in CGI::Cookie

yes, it is broken. the reason is that the pattern is only matching the leading space and nothing else -- ie:

$_ = ' blee blah '; s/\s*(.*?)\s*/$1/; print "matched '$&'; retained '$1'; str is '$_'\n";
which gives matched '       '; retained ''; str is 'blee blah    '

to strip whitespace, the minimal match needs to be forcibly anchored to match to the end of the string, ie:  s/^\s*(.+?)\s*$/$1/, which now successfully strips leading & training whitespace.

however this is a poor way to do it. it is clearer and faster to use 2 regexps, one to strip leading \s, the other the trailing \s; ie:

s/^\s+//; s/\s+$//;

using 2 regexps is also quite a bit faster than the single regexp too:

[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)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://299601]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-08-16 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (167 votes). Check out past polls.

    Notices?