From: Andrew Timberlake Date: 2009-04-10T16:05:21+09:00 Subject: Re: regex woes with breaklines On Fri, Apr 10, 2009 at 1:03 AM, Aa Wilson wrote: > Hello, all.  I've been banging my head against this for a little bit, > and I'm no closer to a solution.  The issue is that I have a string > where I would like to gsub all line ends ('\n' or '\r\n') into '
', > while preserving the line ends.  The catch is that I don't want to gsub > anything that looks like '
\r\n' or '
\n', to prevent my gsub > from inserting breaklines on subsequent edits of this string.  I've > tried gsub with the regex /(?!)($)[^\z]/, but haven't had any > luck getting it to match.  For example, on the string > "test\r\ntest
\r\ntest", it will match both of the '\r\n' sets, > instead of only the first one (which would be optimal). > > Thanks in advance for your time. Replace the
along with the new line Also remember that a new line can be CR (Mac), LF (Linux) or CRLF (Windows) s = "test\r\ntest
\r\ntest\ntest\rtest
\ntest" s.gsub(/(?:
)*(?:\r\n|\n|\r)/, "
\n") #=> "test
\ntest
\ntest
\ntest
\ntest
\ntest" Andrew Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain