troubleshooting Question

String.replace where do i add it

Avatar of Member_2_5230414
Member_2_5230414 asked on
Visual Basic.NET
4 Comments1 Solution334 ViewsLast Modified:
I would like to add  strTextToReplace = smile.Replace(smile, "smile") to the below code

Imports System.Text.RegularExpressions

Public Class BBcode

    Private Shared bbCodeRegex As Regex
    Private Shared bbUnknownRegex As Regex
    Private Shared urlRegex As Regex
    Private Shared urlAnchorRegex As Regex
    Private Shared imgRegex As Regex
    Private Shared boldRegex As Regex
    Private Shared italicRegex As Regex
    Private Shared underRegex As Regex
    Private Shared fSizeRegex As Regex
    Private Shared fColorRegex As Regex
    Private Shared quoteRegex As Regex
    Private Shared smile As String = ":)"

    Shared Sub New()
        bbCodeRegex = New Regex("\[(\w+)[^\]]*\]((?:[^\[]|\[(?!/\1\]))+)\[/\1\]", RegexOptions.Compiled)
        bbUnknownRegex = New Regex("\[(\w+)[^\]]*\]((?:[^\[]|\[(?!/\1\]))+)\[/\1\]", RegexOptions.Compiled)
        urlRegex = New Regex("\[url\]([^\]]+)\[\/url\]", RegexOptions.Compiled)
        urlAnchorRegex = New Regex("\[url=([^\]]+)\]([^\]]+)\[\/url\]", RegexOptions.Compiled)
        imgRegex = New Regex("\[img\]([^\]]+)\[\/img\]", RegexOptions.Compiled)
        boldRegex = New Regex("\[b\]((?:[^\[]|\[(?!/b\]))+)\[/b\]", RegexOptions.Compiled)
        italicRegex = New Regex("\[i\]((?:[^\[]|\[(?!/i\]))+)\[\/i\]", RegexOptions.Compiled)
        underRegex = New Regex("\[u\]((?:[^\[]|\[(?!/u\]))+)\[\/u\]", RegexOptions.Compiled)
        fSizeRegex = New Regex("\[size=([^\]]+)\]((?:[^\[]|\[(?!/size\]))+)\[\/size\]", RegexOptions.Compiled)
        fColorRegex = New Regex("\[color=([^\]]+)\]((?:[^\[]|\[(?!/color\]))+)\[\/color\]", RegexOptions.Compiled)
        quoteRegex = New Regex("\[quote=([^\]]+)\]((?:[^\[]|\[(?!/quote\]))+)\[\/quote\]", RegexOptions.Compiled)

    End Sub

    Shared Function BBcode(ByVal strTextToReplace As String) As String
        Dim mMatch As Match = bbCodeRegex.Match(strTextToReplace)

        While mMatch.Success
            Select Case mMatch.Groups(1).ToString
                Case "url"
                    '//Regex for URL tag without anchor
                    strTextToReplace = urlRegex.Replace(strTextToReplace, "<a href=""$1"">$1</a>")

                    '//Regex for URL with anchor
                    strTextToReplace = urlAnchorRegex.Replace(strTextToReplace, "<a href=""$1"">$2</a>")

                Case "img"
                    '//Image regex
                    strTextToReplace = imgRegex.Replace(strTextToReplace, "<img src=""$1"" />")

                Case "b"
                    '//Bold text
                    strTextToReplace = boldRegex.Replace(strTextToReplace, "<b>$1</b>")

                Case "i"
                    '//Italic text
                    strTextToReplace = italicRegex.Replace(strTextToReplace, "<i>$1</i>")

                Case "u"
                    '//Underline text
                    strTextToReplace = underRegex.Replace(strTextToReplace, "<u>$1</u>")

                Case "size"
                    '//Font size
                    strTextToReplace = fSizeRegex.Replace(strTextToReplace, "<font size="" $1"">$2</font>")

                Case "color"
                    '//Font color
                    strTextToReplace = fColorRegex.Replace(strTextToReplace, "<font color=""$1"">$2</font>")

                Case "quote"
                    strTextToReplace = quoteRegex.Replace(strTextToReplace, "<div class=""quote"" align=""left""><div align=""left"" class=""quotetitle"">Originally Posted by <b>$1</b></div><i>"" $2 ""</i></div>")
                Case Else
                    strTextToReplace = bbUnknownRegex.Replace(strTextToReplace, "$2")
            End Select

            mMatch = bbCodeRegex.Match(strTextToReplace)
        End While

        BBcode = strTextToReplace
    End Function
End Class

Open in new window


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros