[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

VB.NET and Regular Expressions

I am trying to write a function to replace all LINE BREAKS in a string with HTML, using regular expressions.  The code I am using is as follows:

        ' ToHtml
        Public Shared Function ToHtml(ByVal rawText As String) As String

            ' Variable declaration
            Dim result As String = String.Empty

            ' Replace cariage returns with a '<br>'
            result = Regex.Replace(rawText, "\b\r\n", "<br>")

            ' Return value
            Return result

        End Function ' ToHtml

Any reason why this would not work correctly?

Thanks!
0
creativelogic
Asked:
creativelogic
  • 3
  • 3
1 Solution
 
Fernando SotoCommented:
Hi creativelogic;

The only reason that this is not working for you is that the LINE BREAKS in the string are not \r\n as in DOS standard but \n as in Unicode standard.

Try setting the pattern to "\b\n" and see if that works for you.

Fernando
0
 
creativelogicAuthor Commented:
Hi Fernando,

That didn't work ... I did however set the pattern to be ControlChars.NewLine, and that seems to work, but shouldn't the string representation work as well?

Thanks!

Matt
0
 
Fernando SotoCommented:
You state that, "That didn't work ...", then there are no carrage return line feeds or just line feeds in the string.

You state, "I did however set the pattern to be ControlChars.NewLine".
Was this the string you are replacing the \r\n or \n in?
If it is the pattern can you post that code?

Can you post your code please?

Thanks;
Fernando
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZeonFlashCommented:
Would the following work for you?

result = rawText.Replace(vbCrLf, "<br>")
0
 
creativelogicAuthor Commented:
Hi Fernando,

I figured it out ... you put me on the right track though ... I set the pattern to "\r\n" and that worked.   Here is the code that worked:

    ' ToHtml
    Public Shared Function ToHtml(ByVal rawText As String) As String

        ' Variable declaration
        Dim result As String = rawText
        Dim pattern As String = String.Empty

        ' Variable initialization
        pattern = "\r\n"

        ' Replace cariage returns with a '<br>'
        result = Regex.Replace(result, pattern, "<br>")

        ' Return value
        Return result

    End Function ' ToHtml

Thanks!

Matt
0
 
Fernando SotoCommented:
Hi creativelogic;

This is what is happening with the pattern "\b\r\n" and why it is most likely not working for you. If you have a white space before the \r\n then the pattern will not be found because the \b is a word boundry character, meaning that the character before \r\n must not be a white space.

Fernando
0
 
creativelogicAuthor Commented:
Thanks for the help Fernando!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now