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()



        Try
            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
        Do
            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)) = ""
                Else
                    ReDim Preserve Replace(Replace.Length)
                    Replace(UBound(Replace)) = linecol(1)
                End If
            Else
                Exit Do
            End If
        Loop
        sr.Close()

        For k As Integer = 0 To UBound(find) - 1
            find(k) = find(k + 1)
            Replace(k) = Replace(k + 1)
        Next
        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)
        Next
        RTBsource.Text = converted
end sub

how to dynamically add controls to a form

i read through a file path to get all the file names & counts first:

    'retrive file names and counts from server to use to dynamically create convertion buttons.
    Private Sub getFileNames()
        For Each foundfile As String In My.Computer.FileSystem.GetFiles(filepath)
            Dim fName As String = My.Computer.FileSystem.GetName(foundfile)
            fName = fName.Remove(fName.Length - 4)
            ReDim Preserve Filterfile(Filterfile.Length)
            Filterfile(UBound(Filterfile)) = fName.Substring(3)
        Next
        For k As Integer = 0 To UBound(Filterfile) - 1
            Filterfile(k) = Filterfile(k + 1)
        Next
        ReDim Preserve Filterfile(UBound(Filterfile) - 1)
        FileCount = Filterfile.Length
    End Sub

then using the counts and filenames to add tooltipstrip menuitems dynamically.


   Private Sub CreatemenuitemsArray()
        Dim ndx As Short
        Dim toolstriparray(FileCount - 1) As System.Windows.Forms.ToolStripMenuItem
        'Destroy any previous instances
        For ndx = 0 To FileCount - 1
            If ConversionsToolStripMenuItem.DropDownItems.Contains(toolstriparray(ndx)) Then
                With toolstriparray(ndx)
                    RemoveHandler .Click, AddressOf Me.toolstriparray_Click 'Detatch handler reference
                End With
                ConversionsToolStripMenuItem.DropDownItems.Remove(toolstriparray(ndx))
                toolstriparray(ndx).Dispose()
            End If
        Next

        'Create textbox controls
        For ndx = 0 To FileCount - 1
            toolstriparray(ndx) = New System.Windows.Forms.ToolStripMenuItem()
            With toolstriparray(ndx)
                .Text = Filterfile(ndx)
                AddHandler .Click, AddressOf Me.toolstriparray_Click 'Attach handler reference
            End With
        Next
        ConversionsToolStripMenuItem.DropDownItems.AddRange(toolstriparray)
    End Sub

    Private Sub toolstriparray_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        'click event handler, calls the update sub to format the html
        Dim file As ToolStripMenuItem
        Try
            file = DirectCast(sender, ToolStripMenuItem)
        Catch ex As Exception
            Exit Sub
        End Try
        Dim fName As String
        fName = "RE_" & file.Text & ".txt"
        Dim filt As String = filepath & fName
        Try
            UpdateHTML(filt)
        Catch ex As Exception
            Exit Sub
        End Try
        wbHTML.Document.Body.InnerHtml = bodycss & RTBsource.Text
    End Sub


Note: wbHTML is a web browser control.

Web browser controls

this morning i encounted a problem with web browser control:

webcontrol.documenttext returns funny popup messages everytime i change something in design or source code.

then i changed to webcontrol.document.body.innerhtml and it worked perfectly