in reply to regex to replace linefeeds with <p> tags

thanks to all for the great suggestions. they're all very helpful.

liverpole, i agree with you, and i was thinking that i would just leave out the linefeeds, but when the posts are long, i don't like seeing the text all strung together without easily seeing the paragraph breaks - just a preference thing.

a general question about the \r ? that both Joost and throop suggest......i started out with \n{2,} but found that some of my users were cutting an pasting from word processors, and that introduced the occasional \r into the mix. so, will [ (\r? \n){2,} ] match to "\r\r" ? that was what i was hoping would work with the \r\n{2,} - that it would match to \r\r or \r\n or \n\r or \n\n (as well as \r\r\n and \r\n\r and \r\n\n and \n\n\r etc etc etc.....)

clearly, passing through twice, and changing any \r to \n and then matching the \n{2,} to replace to the para tags would be reliable, but seems inefficient.

Replies are listed 'Best First'.
Re^2: regex to replace linefeeds with <p> tags
by throop (Chaplain) on Dec 26, 2006 at 03:48 UTC
    Pasting from Windows environments will introduce \r\n because that's what Microsoft uses for linebreaks. It won't introduce \n\r or \r\r.

    You don't want to introduce a <p> from a single <RETURN>, right? \r?\n is what you want to match.

    If it's clearer to you, go ahead and remove all the \r in one pass and then handle the \n. Don't worry about efficiency here — you're doing IO!. The number of CPU cycles it takes to get a response from the keyboard to the CPU is enormous in comparison to the cycles to do a string replace.

    throop