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
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE