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!
creativelogicAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
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
 
Fernando SotoRetiredCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Fernando SotoRetiredCommented:
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
 
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
 
creativelogicAuthor Commented:
Thanks for the help Fernando!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.