in reply to Re: Regex Question
in thread Regex Question

s/([^\n])\n([^\n])/$1$2/g;
That fails on fred\nX\nY\nbarney, since the X will be sucked up while fixing the preceding newline, and won't be available to match for the following newline.

Be very wary when matching right-side context. Passing it through to the "already seen" category means it won't be able to be left-side context for a later match.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re (tilly) 3: Regex Question
by tilly (Archbishop) on Jan 08, 2001 at 00:11 UTC
    That is fixable though with a lookahead:
    s/([^\n])\n(?!\n)/$1/g;
    Or the probably faster lookbehind:
    s/(?<!\n)\n([^\n])/$1/g;

    UPDATE
    chipmunk noticed a typo. I had not closed the second match. Oops.