RegEx to replace a string [modified]
-
I have a text field where users can submit source code to my website. This field contain the mix of code and description. All source code will be enclosed within a special tag [CODE][/Code] I am looking for the regular expression to do the following in VB.NET: Replace all occurrences of [code] with <pre> Replace all occurrences of [/code] with </pre> Replace all linebreaks (vbCrLf) outside the [code][/code] blocks with <BR> Replace all < inside the [code][/code] blocks with ∓ -- modified at 10:09 Saturday 24th February, 2007
-
I have a text field where users can submit source code to my website. This field contain the mix of code and description. All source code will be enclosed within a special tag [CODE][/Code] I am looking for the regular expression to do the following in VB.NET: Replace all occurrences of [code] with <pre> Replace all occurrences of [/code] with </pre> Replace all linebreaks (vbCrLf) outside the [code][/code] blocks with <BR> Replace all < inside the [code][/code] blocks with ∓ -- modified at 10:09 Saturday 24th February, 2007
Public Function Change_Text(ByVal strInText As String, ByVal strOutText As String) As Boolean Dim strSubText As String Dim intPos As Integer Dim bolCode As Boolean = False 'Default as OK Change_Text = True 'Clear output string strOutText = "" 'Loop through string Do Until strInText = "" 'Look for opening '[' If Mid(strInText, 1, 1) = "[" Then 'Find closing tag intPos = InStr(2, strInText, "]") 'Extract content strSubText = Mid(strInText, 2, intPos - 2) 'Skip text beyond end tag strInText = Mid(strInText, intPos + 1) 'Check tag information.... Select Case UCase(strSubText) Case "/CODE" 'Closing tag strOutText += "" 'Flag outside code area bolCode = False Case "CODE" 'Opening tag strOutText += "
" 'Flag inside code area bolCode = True Case Else 'Unknown [] tag - Flag as error ... Change_Text = False End Select ElseIf Mid(strInText, 1, 2) = vbCrLf Then strInText = Mid(strInText, 3) If bolCode Then 'Inside Code Tags strOutText += "∓" Else 'Outside Code Tags strOutText += "" End If Else 'Write character to output string... strOutText += Mid(strInText, 1, 1) End If Loop End Function
-
Public Function Change_Text(ByVal strInText As String, ByVal strOutText As String) As Boolean Dim strSubText As String Dim intPos As Integer Dim bolCode As Boolean = False 'Default as OK Change_Text = True 'Clear output string strOutText = "" 'Loop through string Do Until strInText = "" 'Look for opening '[' If Mid(strInText, 1, 1) = "[" Then 'Find closing tag intPos = InStr(2, strInText, "]") 'Extract content strSubText = Mid(strInText, 2, intPos - 2) 'Skip text beyond end tag strInText = Mid(strInText, intPos + 1) 'Check tag information.... Select Case UCase(strSubText) Case "/CODE" 'Closing tag strOutText += "" 'Flag outside code area bolCode = False Case "CODE" 'Opening tag strOutText += "
" 'Flag inside code area bolCode = True Case Else 'Unknown [] tag - Flag as error ... Change_Text = False End Select ElseIf Mid(strInText, 1, 2) = vbCrLf Then strInText = Mid(strInText, 3) If bolCode Then 'Inside Code Tags strOutText += "∓" Else 'Outside Code Tags strOutText += "" End If Else 'Write character to output string... strOutText += Mid(strInText, 1, 1) End If Loop End Function