in reply to removing redundantwhitespace

perlretut
How do I remove whitespace at the beginning or end of my string?
whitespaces replace with tabs - help needed...
global whitespace delete
Removing Trailing Whitespace: Multiple ways.

Replies are listed 'Best First'.
Re^2: removing redundantwhitespace
by Anonymous Monk on Sep 13, 2008 at 13:16 UTC
    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
      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.