in reply to (Regex Madness) And you thought whitespace was easy.

This is cheating a little, but quicker:
# If you have any spare characters to use # (lets say '~') sub my_squish { local $_ = shift; s/\n{2}/~/g; s/\s+/ /g; s/~/\n\n/g; $_; }
Update: True enough japhy, but I said I was cheating :) And even though it scans the string three times, its simple and quicker than doing the reversing, lookaheads & lookbehinds necessary to do it in one regex.

Replies are listed 'Best First'.
Re: Re: (Regex Madness) And you thought whitespace was easy.
by japhy (Canon) on Aug 15, 2001 at 09:24 UTC
    It requires you to scan your string for a substring not yet used. That requires you scan the string at least once, or use a string that is one character LONGER than your string.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      How about this then:
      join "\n\n", map { s/\s+/ /; $_ } split(/\n{2}/, $_, -1);