Avatar of Member_2_5230414
Member_2_5230414

asked on 

String.replace where do i add it

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


Visual Basic.NET

Avatar of undefined
Last Comment
Hairbrush
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

You are using regex (a number of them). Is that not working?
logically strTextToReplace = smile.Replace(smile, "smile")
will be same as
strTextToReplace = "smile"
Avatar of Member_2_5230414
Member_2_5230414

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Hairbrush
Hairbrush
Flag of Jersey 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
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