Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. RegEx to replace a string [modified]

RegEx to replace a string [modified]

Scheduled Pinned Locked Moved Visual Basic
regexcsharp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T John
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • T T John

      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

      T Offline
      T Offline
      The Tigerman
      wrote on last edited by
      #2

      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 += "&mp;"
                      Else
                          'Outside Code Tags
                          strOutText += ""
                      End If
                  Else
                      'Write character to output string...
                      strOutText += Mid(strInText, 1, 1)
                  End If
              Loop
          End Function
      
      T 1 Reply Last reply
      0
      • T The Tigerman

        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 += "&mp;"
                        Else
                            'Outside Code Tags
                            strOutText += ""
                        End If
                    Else
                        'Write character to output string...
                        strOutText += Mid(strInText, 1, 1)
                    End If
                Loop
            End Function
        
        T Offline
        T Offline
        T John
        wrote on last edited by
        #3

        Thank you. The above code will work, but I am looking for regular expression which can do the above in 1~3 lines and which would be nore efficient.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups