Shawn H Corey wrote:
sftriman wrote:
I use this series of regexp all over the place to clean up lines of
text:
$x=~s/^\s+//g;
$x=~s/\s+$//g;
$x=~s/\s+/ /g;
in that order, and note the final one replace \s+ with a single space.
Basically, it's (1) remove all leading space, (2) remove all trailing
space,
and (3) replace all multi-space with a single space [which, at this
point,
should only occur on interior characters].
Is there a handy way to do this in one regexp? And, fast? I've been
using Devel::NYTProf to study code timing and see that some regexp,
especially mine, can be CPU expensive/intensive.
tr/// is generally faster than s///
$text =~ tr{\t}{ };
$text =~ tr{\n}{ };
$text =~ tr{\r}{ };
$text =~ tr{\f}{ };
$text =~ tr{ }{ }s;
That can be reduced to:
$text =~ tr/ \t\n\r\f/ /s;
But that still doesn't remove leading and trailing whitespace so add two
more lines:
$text =~ tr/ \t\n\r\f/ /s;
$text =~ s/\A //;
$text =~ s/ \z//;
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/