in reply to Re: removing redundantwhitespace
in thread removing redundantwhitespace

Hi,
I've looked at these but they don't help me when my string contains "\n".
So my example again -
one two   \n\n   three\t\t\n four    five\n\n\n\t six \n\n
needs to become-
one two\nthree\nfour five\nsix

Replies are listed 'Best First'.
Re^3: removing redundantwhitespace
by JadeNB (Chaplain) on Sep 13, 2008 at 21:03 UTC
    It seems to me that you want a run of whitespace that includes a newline to become a single newline, and a run of spaces to become a single space. It's not clear to me what you want to happen to a run of whitespace that includes a tab, but no newline; let's suppose that you want that to collapse to just a space, too. If your entire string (including newlines) is in the scalar $string, then
    $string =~ s/\s*\n\s*/\n/g; $string =~ s/\s+/ /g;
    should do it. This will not destroy leading and trailing whitespace; for that, you should use ikegami's solution, or read again the links in Re: removing redundantwhitespace.