http://www.perlmonks.org?node_id=413713


in reply to regexp for scalar containing a mixture of LF & CRLF

I think what you want is
s/(?<!\r)\n/\r\n/;
The problem with your code is that you're trying to replace a single CR with an LF, so if you have an LF alone, it's unchanged. If you start with CRLF, you end up with LFLF, which isn't what you want. What you need to look for is an LF not preceded by CR, and replace it with CRLF.

Update: It appears I spoke a little too soon, and with too much Unix-centrism. The text was fine, but I forgot that "\n" in Perl changes meaning under different platforms. What you want is

s/(?<!\015)\012/\015\012/g;

Replies are listed 'Best First'.
Re^2: regexp for scalar containing a mixture of LF & CRLF
by NateTut (Deacon) on Dec 09, 2004 at 23:59 UTC
    Thanks, but nope. See my next post for my ugly answer that finally worked.

    Doug