in reply to Removing empty line(s) with regex in a multiple strings in a variable

Your regex  s/[\s]+//mgx deletes all whitespace, and you don't want that -- you want to preserve the spaces that separate the four strings on the line that starts with ">".

(I could also point out that in your regex, the square brackets and "m" and "x" modifiers could all be removed and it would do the same thing it does now -- including them has no effect at all in this case. Of course, this also means they do no harm, but if you don't understand why they do nothing in this case, it wouldn't hurt you to try reading the perlre manual page.)

As GrandFather points out, you only want to eliminate the extra "\n" characters. The specific output that you say you want (with the initial "\n" still intact) would come out with just this:

s/\n\s*/\n/g; # every string of \n plus 0-or-more whitespace --> sing +le \n
(updated to emulate GrandFather's handling of "blank" lines that contain just spaces and/or tabs)

Replies are listed 'Best First'.
Re^2: Removing empty line(s) with regex in a multiple strings in a variable
by Strazhnik (Initiate) on Apr 27, 2010 at 15:14 UTC
    I worked with the same trouble, and the only one good working way I found, it is $buffer =~ s/^\s*\n+//mg; Thank you!