in reply to Regex Question

The easiest way to fix this substitution is to capture the extra characters: s/([^\n])\n([^\n])/$1$2/g; Although negative lookahead and/or lookbehind could be used instead: s/(?<!\n)\n(?!\n)//g; Lookahead and lookbehind assertions make sure that the subpattern matches (or doesn't match, for negative assertions), without using up those characters in the actual match. perlre explains it better. :)

Replies are listed 'Best First'.
Re: Re: Regex Question
by merlyn (Sage) on Jan 08, 2001 at 00:02 UTC
    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

      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.