Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
regex woes with breaklines
Aa Wilson
4/9/2009 11:03:00 PM
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 '<br />',
while preserving the line ends. The catch is that I don't want to gsub
anything that looks like '<br />\r\n' or '<br />\n', to prevent my gsub
from inserting breaklines on subsequent edits of this string. I've
tried gsub with the regex /(?!<br\s*\/>)($)[^\z]/, but haven't had any
luck getting it to match. For example, on the string
"test\r\ntest<br>\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.
--
Posted via
http://www.ruby-...
.
3 Answers
Andrew Timberlake
4/10/2009 7:05:00 AM
0
On Fri, Apr 10, 2009 at 1:03 AM, Aa Wilson <aawilso1@vt.edu> wrote:
> Hello, all. =A0I've been banging my head against this for a little bit,
> and I'm no closer to a solution. =A0The issue is that I have a string
> where I would like to gsub all line ends ('\n' or '\r\n') into '<br />',
> while preserving the line ends. =A0The catch is that I don't want to gsub
> anything that looks like '<br />\r\n' or '<br />\n', to prevent my gsub
> from inserting breaklines on subsequent edits of this string. =A0I've
> tried gsub with the regex /(?!<br\s*\/>)($)[^\z]/, but haven't had any
> luck getting it to match. =A0For example, on the string
> "test\r\ntest<br>\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 <br> along with the new line
Also remember that a new line can be CR (Mac), LF (Linux) or CRLF (Windows)
s =3D "test\r\ntest<br>\r\ntest\ntest\rtest<br/>\ntest"
s.gsub(/(?:<br *\/*>)*(?:\r\n|\n|\r)/, "<br />\n")
#=3D> "test<br />\ntest<br />\ntest<br />\ntest<br />\ntest<br />\ntest"
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...
"I have never let my schooling interfere with my education" - Mark Twain
Aa Wilson
4/10/2009 12:52:00 PM
0
Andrew Timberlake wrote:
> s = "test\r\ntest<br>\r\ntest\ntest\rtest<br/>\ntest"
> s.gsub(/(?:<br *\/*>)*(?:\r\n|\n|\r)/, "<br />\n")
> #=> "test<br />\ntest<br />\ntest<br />\ntest<br />\ntest<br />\ntest"
Thanks a million. It feels like a waste replacing something that's
already there, but I suppose that's just my conceptions of real life
getting in the way of my abstractions.
I was actually hoping that '$' (or possibly '$$?') would cover all the
CR/LF/CRLF cases for me, was I mistaken about that?
--
Posted via
http://www.ruby-...
.
Robert Klemme
4/10/2009 9:43:00 PM
0
On 10.04.2009 14:52, Aa Wilson wrote:
> Andrew Timberlake wrote:
>
>> s = "test\r\ntest<br>\r\ntest\ntest\rtest<br/>\ntest"
>> s.gsub(/(?:<br *\/*>)*(?:\r\n|\n|\r)/, "<br />\n")
>> #=> "test<br />\ntest<br />\ntest<br />\ntest<br />\ntest<br />\ntest"
>
> Thanks a million. It feels like a waste replacing something that's
> already there, but I suppose that's just my conceptions of real life
> getting in the way of my abstractions.
Not necessarily: with 1.9's regular expression engine there is negative
lookbehind as well. You could use that to prevent substitution of
newlines which are preceeded by a <br/> already.
http://www.geocities.jp/kosako3/oniguruma/...
> I was actually hoping that '$' (or possibly '$$?') would cover all the
> CR/LF/CRLF cases for me, was I mistaken about that?
I believe it does not cover \r alone. In every case it is safer to be
explicit IMHO. Btw, I believe you can simplify to (?:\r\n?|\n).
Kind regards
robert
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
regex woes with breaklines
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password