Monday, December 13, 2010

find and replace regular expressions

using a ".txt" file stored on a particular path, and read the file when trying to replace the regex.
I use comma as a split delimiter in rows to separate one row into two columns. so that
string(0) = finding regex, string(1) = replacement

    Private Sub UpdateHTML(ByVal FilterFile As String)
        Dim line As String
        Dim sr As StreamReader = Nothing
        Dim converted As String

        converted = SaveHtml()

            sr = New StreamReader(FilterFile)
        Catch ex As Exception
            Exit Sub
        End Try
        'Replacing regex using .txt file stored on server-
        Dim find(0) As String
        Dim Replace(0) As String
            line = sr.ReadLine()
            If Not line Is Nothing Then
                Dim linecol() As String
                linecol = line.Split(",")
                ReDim Preserve find(find.Length)
                find(UBound(find)) = linecol(0)
                If linecol(1) = "empty" Then
                    ReDim Preserve Replace(Replace.Length)
                    Replace(UBound(Replace)) = ""
                    ReDim Preserve Replace(Replace.Length)
                    Replace(UBound(Replace)) = linecol(1)
                End If
                Exit Do
            End If

        For k As Integer = 0 To UBound(find) - 1
            find(k) = find(k + 1)
            Replace(k) = Replace(k + 1)
        ReDim Preserve find(UBound(find) - 1)
        ReDim Preserve Replace(UBound(Replace) - 1)

        For i As Integer = 0 To find.Length - 1
            Dim regex As New Regex(find(i), RegexOptions.Multiline Or RegexOptions.Compiled Or RegexOptions.IgnoreCase)
            Dim regexReplace As String = Replace(i)
            converted = regex.Replace(converted, regexReplace)
        RTBsource.Text = converted
end sub

No comments:

Post a Comment