in reply to Re: regex to replace linefeeds with <p> tags
in thread regex to replace linefeeds with <p> tags

Those are good suggestions, but I would do this:
s/(\r?\n){2,}/\n<\/p>\n<p>\n/g
to make the \r optional, instead of doing 2 passes over the data. (and I added a \n in the replacement to make the results look a little nicer in my eyes)

Replies are listed 'Best First'.
Re^3: regex to replace linefeeds with <p> tags
by throop (Chaplain) on Dec 25, 2006 at 22:56 UTC
    I'll second Joost's approach, but make a few suggestions for readability / maintainability.
    • Use qw so to de-clutter the list of strings.
    • Use an explicit variable in the for; they are cheap and they make your intention clear.
    • Use [ ] braces for the the regex separator so you won't have to backslash the slash. This de-emphasizes some of the executable line noise effect
    • Use the regex x modifier to put some whitespace and comments in here.
    foreach my field (qw(postby title teaser content)){ $in{$field} =~ s[ (\r? \n){2,} ] # two or more CR [ \n </p> \n <p> \n]gx; # Close one para, open ano +ther }
    throop
      Use [ ] braces for the the regex separator so you won't have to backslash the slash. This de-emphasizes some of the executable line noise effect
      I've long been of two minds about this. Consistency makes for readability, and I don't think I could sell everyone I share code with on always using some other delimiter, so I see a lot of advantage to always using / and escaping as needed (and the escaping does the job of pointing out that the following / is indeed literal, which is just what I want done.) On the other hand, that \ seems to bother a lot of other people, and they can't all be wrong, can they?
Re^3: regex to replace linefeeds with <p> tags
by ysth (Canon) on Dec 25, 2006 at 23:43 UTC
    I didn't suggest that because then you are (assuming the data was consistent in the first place) leaving most lineends as "\r\n" but those at paragraph breaks as "\n", and that bothered me.