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.

The g-modifier on the first 2 is bogus
(unless you would add an m-modifier).

I currently tend to write it like this:

    s/\s+\z//, s/\A\s+//, s/\s+/ /g, for $x;

So first remove tail spaces (less to lshift next).
Then remove head spaces. Then normalize.


For a multi-line buffer you can do it like this:

perl -wle '

  my $x = <<"EOT";
    123    456   \t
abc def
\t\t\t\t        \t\t\t\t
   ***     ***   ***   \t
EOT

  s/^\s+//mg, s/\s+$//mg, s/[^\S\n]+/ /g for $x;

  $x =~ s/\n/>\n/g;
  print $x, "<";
'

123 456>
abc def>
*** *** ***<

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to