Regular Expression or Code

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

I have a problem and I am looking for suggestions. Here is the situation:

I am writing a VB.NET app to convert a string into a markup language used in
a program we have at my company. The languange handles CrLf's very stragly.
It doesn't ignore them like HTML. It will read and print them when there
are two or more in a row, but it ignores a single CRLF. To do a single line
break, you need to use the command .br; however, two CRLF's register as two
line breaks. That's right. I said two. Oh, just to make things a little
more challenging, replacing every CRLF with a .br doesn't work. The app
only reads the first .br and ignores the rest..

I can't say much for the markup languange , but it is what I have to work
with. Here is what I need to do when I parse the string:

I need to replace a single CRLF with a .br.
I need to leave two or more CRLFs in a row the way they are.

I am looking for a regular expression that would accomplish this, but code
that does the same thing would work too. In other words, any suggestions
would be appreciated.

Thanks,
Matt
 
Matt,

To make it more clear to me, you need to do something as

mytext = mytext.replace(" " & VBCRLF & " ",".br")

Cor
 
Doh, error

mytext = mytext.replace(VBCRLF & VBCRLF, "@#AnonexistingText&^")
mytext = mytext.replace(VBCRLF, ".br")
mytext = mytext.replace("@#AnonexistingText&^", VBCRLF & VBCRLF)

Keep in mind that the replace is moslty more than 100 times faster than the
regex.

I hope this helps?

Cor
 
Very, very nearly what I needed. Your approach helped me find the solution.
I only had to add one more replace to make sure the .br wasn't there AT ALL
if there was more then one vbCrLf:

mytext = mytext.replace(vbCrLf & vbCrLf, "@#AnonexistingText&^")
mytext = mytext.Replace(vbCrLf, vbCrLf & ".br" & vbCrLf)
mytext = mytext.Replace("@#AnonexistingText&^", vbCrLf & vbCrLf)
mytext = mytext.Replace(vbCrLf & vbCrLf & ".br", vbCrLf)
 
Matt,
With 4 or more replaces in a row like that I would consider using a
StringBuilder instead of re-creating the string each time.

Something like:

Dim sb As New System.Text.StringBuilder(mytext, mytext.Length * 2)

sb.Replace(vbCrLf & vbCrLf, "@#AnonexistingText&^")
sb.Replace(vbCrLf, vbCrLf & ".br" & vbCrLf)
sb.Replace("@#AnonexistingText&^", vbCrLf & vbCrLf)
sb.Replace(vbCrLf & vbCrLf & ".br", vbCrLf)

mytext = sb.ToString()

The "mytext.Length * 2" is how big a buffer the StringBuilder is going to
work with, I would attempt to calculate the largest result string
(intermediate also) that would occur from the Replacements...

With 3 or 4 replacements I would probably go with your code as is, unless
there was a performance problem, then I would try the StringBuilder to see
if that helped or hurt.

Hope this helps
Jay
 

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

Back
Top