If you're using Perl 5.10 or later, try this:
$string =~ s/(\R)(?:\h*\R)+/$1$1/g;
\R
is the generic line-separator escape sequence (ref), and \h
matches any horizontal whitespace character (e.g. space and TAB) (ref). So this will convert any sequence of one or more blank lines to one empty line.
Most applications these days are liberal in what they'll recognize as a line separator; they'll even accept a mix of two or more styles of separator in the same document. On the other hand, some apps actively convert all line separators to one preferred style. But sometimes you do have to stick to one particular style; that's why I captured the first \R
match and used it as the replacement, instead of arbitrarily using \n
.
Be aware that these special escape sequences aren't widely supported in other regex flavors. They work in recent versions of PHP, and \R
seems to work in Ruby 2.0, though I can't find any doc that mentions it. Ruby 1.9.2 and 2.0 support a \h
escape sequence, but it matches a hexadecimal digit ([0-9a-fA-F]
), not horizontal whitespace. In most other flavors, \R
and \h
will either throw an exception or match a literal R
and h
respectively.