> Is the following regrex the correct way to remove leading or
> trailing white space
> from a string?
>
> $data = " [EMAIL PROTECTED] (mike smith) "
>
> $data =~ s/(^\s+)|(\s+$)//g;
This looks like it works to me.
> or would it be more efficient to it thus:
>
> # two passes
> $data =~ s/^\s+)//;
> $data =~ s/\s+$)//;
I ran the following code:
use Benchmark;
use strict;
use warnings;
timethese (1000000,
{ pipe => q{
my $data = " bla\@bla.net (mike smith) ";
$data =~ s/(^\s+)|(\s+$)//g;
} ,
separate => q{
my $data = " bla\@bla.net (mike smith) ";
$data =~ s/^\s+//;
$data =~ s/\s+$//;
}
}
);
And got this output...
Benchmark: timing 1000000 iterations of pipe, separate...
pipe: 19 wallclock secs (18.02 usr + 0.00 sys = 18.02 CPU) @
55478.50/s (n=1000000)
separate: 3 wallclock secs ( 3.72 usr + 0.00 sys = 3.72 CPU) @
269106.57/s (n=1000000)
So I think breaking it into 2 lines is much more efficient.
> Final comment when I am reading from a loop:
>
>
> open(EL,"$emailLog") || &errorLog( "Read in, Could not find
> $emailLog, $!");
> my @lines = <EL>;
>
> foreach(@lines){
> next if /test/;
> $_ =~ s/^\s+(.*)\s+$/\1,/;
>
> .... other code....
>
> Do I need to escape the '@' as my data will be of format:
> [EMAIL PROTECTED] (name)
No, maybe someone else can explain what's going on inside that avoids this,
but I could swear I've done this sort of thing before and had no problems
with my variable contents being interpolated as lists.
BTW s/// operate on $_ by default, just like the m// in the 'next if' line.
So you could just do
s/^\s+(.*)\s+$/\1,/;
on that line.
Also $1 is preferred over \1 according to my 5.6.1 version.
Peter C.