String.replace where do i add it

Member_2_5230414
Member_2_5230414 used Ask the Experts™
on
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"
                    '//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


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
You are using regex (a number of them). Is that not working?
Imran Javed ZiaConsultant Software Engineer - .NET Architect

Commented:
logically strTextToReplace = smile.Replace(smile, "smile")
will be same as
strTextToReplace = "smile"

Author

Commented:
yes but when i add it like this (i forgot to chnage smile to :)) it replaces EVERYTHING to the work smile

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"
                    '//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)
            strTextToReplace = smile.Replace(smile, "smile")
        End While




        BBcode = strTextToReplace
    End Function
End Class

Open in new window

Hi

As IJZ said, line 78 of your code is equivalent to the following

        Dim smile As String = ":)"
        Dim strTextToReplace = smile.Replace(smile, "smile")
        Console.WriteLine(strTextToReplace) 'value of strTextToReplace is "smile"

That's because the way you've written the Replace it will discard the previous contents of strTextToReplace.

I think what you want to do is replace :) with the word smile wherever it appears in strTextToReplace?  If that's what you want to do, try this:

       strTextToReplace = strTextToReplace.Replace(smile,"smile")

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial