asked on
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
ASKER
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
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,
TRUSTED BY