Avatar of creativelogic
creativelogic

asked on 

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!
Visual Basic.NET

Avatar of undefined
Last Comment
creativelogic
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
Avatar of creativelogic
creativelogic

ASKER

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
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
Avatar of ZeonFlash
ZeonFlash

Would the following work for you?

result = rawText.Replace(vbCrLf, "<br>")
Avatar of creativelogic

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of creativelogic

ASKER

Thanks for the help Fernando!
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo