RegEx for changing linefeeds to <BR> except between <PRE></PRE> tags?

R

Rocky Moore

I have a web site called HintsAndTips.com. On this site people post tips
using a very simply webform with a multi line TextBox for inputing the tip
text. This text is encode to HTML so that no tags will remain making the
page safe (I have to convert the linefeeds to <BR>s because the
Server.EncodeHTML does not do that it seems).

The problem is that users can use a special tag when editing the top to
specify an area of the tip that will be blocked into a scrollable div with a
pre tag so that the lines do not wrap, but that it does not spread beyond
the width of the content area of the tip. As an example, here is a tip that
uses this tag which is replaced with the div and pre tags as needed:

http://hintsandtips.com/hat.aspx?P=ShowTip&C=31&F=100&T=10

If the text in that special area is wider than the page it will scroll and
not break the formatting of the page. But as you can see there are no
linefeeds in that special section which do exist in the tip itself (but now
as <BR>s).

So, I need to build a RegEx expression that will replace all linefeeds
except between <pre></pre> tags. Would also be nice if it would only do
that if the <pre></pre> tags are balanced so that it will do nothing if one
is missing.

Is this even possible? Do I need to parse the text line-by-line or maybe it
is just something really simple I am overlooking?

Any help appreciated!
 
C

Curt_C [MVP]

1) Replace the BR's
2) Replace the "special" tags with something like "||"
3) Encode
4) Replace the "||" back with the "<pre></pre>" tags
 
R

Rocky Moore

Confused, how does that maintain the linefeeds between the "<pre></pre>"
tags?

I do not have a problem with the special tag itself being changed in the
encode process since I simple replace the "&lt;FIXED&gt;" string ;)
 
C

Curt_C [MVP]

I had 1 & 2 backwards.... but you already had that taken care of....
I guess if you already can change these back then what's the issue?

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
 
R

Rocky Moore

The issue (and the focus of the post) is how to limit the changes of the
<BR> between the "<pre></pre>" tags as the subject line says ;)

I need to change all the linefeeds to <BR>'s unless it is between my special
tag (which is replaced with a <pre> tags) Simple replaces will replace all
of them. If a tip has one or more of the special sections, those sections
have to be excluded in the replacing of the linefeeds.
 
D

Dan Brussee

Thinking out loud... umm... online... whatever...

Find the text between <pre> and </pre> and store it in a var. Then
replace all occurnaces of linefeeds with <BR>. Then replace the
<pre></pre> (now empty) with the value stored.
 
R

Rocky Moore

Yeah, would still be a lot of overhead since there can be multiple tag
sections. You would have to pull all the tag sections and then after you
are done, replace them.

It really seems like a simple task for RegEx, but after quite a bit of
searching on the net, I have yet to find anything close. I found a RegEx
for vb that was supposed to replace the linefeeds except in <pre> tag blocks
but it didn't work with .NET :(

Thanks for the post!
 
M

mikeb

Rocky said:
Yeah, would still be a lot of overhead since there can be multiple tag
sections. You would have to pull all the tag sections and then after you
are done, replace them.

It really seems like a simple task for RegEx, but after quite a bit of
searching on the net, I have yet to find anything close. I found a RegEx
for vb that was supposed to replace the linefeeds except in <pre> tag blocks
but it didn't work with .NET :(

Post the RegEx you tried - maybe we can get it to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top