Perl Regular expression remove double tabs, line breaks, white spaces

Posted by Scoox on Stack Overflow See other posts from Stack Overflow or by Scoox
Published on 2010-12-28T15:36:35Z Indexed on 2010/12/28 15:54 UTC
Read the original article Hit count: 232

Filed under:
|

Hi guys, I want to write a perl script that removes double tabs, line breaks and white spaces.

What I have so far is:

$txt=~s/\r//gs;
$txt=~s/ +/ /gs;
$txt=~s/\t+/\t/gs;
$txt=~s/[\t\n]*\n/\n/gs;
$txt=~s/\n+/\n/gs;

But, 1. It's not beautiful. Should be possible to do that with far less regexps. 2. It just doesn't work and I really do not know why. It leaves some double tabs, white spaces and empty lines (i.e. lines with only a tab or whitespace)

I could solve it with a while, but that is very slow and ugly.

Any suggestions?

© Stack Overflow or respective owner

Related posts about regex

Related posts about perl