Search code examples
regexvb.nettextstreamreader

VB.net read a text file and populate a combobox with specific extracted words


I have a problem which is giving me a headache. I really thought someone would have asked this already, but days of reading and testing has been fruitless.

I have a text file which starts:

"Determining profile based on KDBG search...

     Suggested Profile(s) : WinXPSP2x86, WinXPSP3x86 (Instantiated with WinXPSP2x86)"

(The blank line between the two is not an error and neither are the spaces before 'Suggested')

I need to read the line starting 'Suggested...' only and extract every unique word starting 'Win' and populate a combobox with them. (i.e. 'WinXPSP2x86' and 'WinXPSP3x86')

I know i need to use the 'StreamReader' class and probably get a Regex going on, but, as a beginner, connecting it all together is beyond my knowledge at the moment.

Can anyone help? It would be much appreciated.


Solution

  • Imports System.IO
    Public Class Form1
    
    Private Sub Form1_Load( sender As Object,  e As EventArgs) Handles MyBase.Load
    
        ' BASIC is case sensitive and e is parameter so we will start
        ' new variables with the letter f.
    
        ' Read all lines of file into string array F.
        Dim F As String() = File.ReadAllLines("H:\Projects\35021241\Input.txt")
        ' F() is a 0 based array.  Assign 3 line of file to G.
        Dim G As String = F(2)
        ' On line 3 of file find starting position of the word 'win' and assign to H.
        ' TODO:  If it is not found H will be -1 and we should quit.
        Dim H As Integer = G.IndexOf("Win")
        ' Assign everything beginning at 'win' on line 3 to variable I.
        Dim I As String = G.Substring(H)
        ' The value placed in delimiter will separate remaining values in I.
        ' Place C after ending quote to represent a single character as opposed to a string.
        Dim Delimiter As Char = ","C
        ' J array will contain values left in line 3.
        Dim J As String() = I.Split(Delimiter)
    
        ' Loop through J array removing anything in parenthesis.
        For L = J.GetLowerBound(0) to J.GetUpperBound(0)
            ' Get location of open parenthesis.
            Dim ParenBegin As Integer = J(L).IndexOf("(")
            ' If no open parenthesis found continue.
            If ParenBegin <> -1 then
                ' Open parenthesis found.  Find closing parenthesis location
                ' starting relative to first parenthesis.
                Dim Temp As String = J(L).Substring(ParenBegin+1)
                ' Get location of ending parenthesis.
                Dim ParenEnd As Integer = Temp.IndexOf(")")
                ' TODO:  Likely an exception will be thrown if no ending parenthesis.
                J(L) = J(L).Substring(0,ParenBegin) & J(L).Substring(ParenBegin + ParenEnd +2)
                ' Change to include text up to open parenthesis and after closing parenthesis.
            End If
        Next L
    
        ' UnwantedChars contains a list of characters that will be removed.
        Dim UnwantedChars As String = ",()"""
        ' Check each value in J() for presence of each unwanted character.
        For K As Integer = 0 to (UnwantedChars.Length-1)
            For L = J.GetLowerBound(0) To J.GetUpperBound(0)
                ' Declare M here so scope will be valid at loop statement.
                Dim M As Integer = 0
                Do
                    ' Assign M the location of the unwanted character or -1 if not found.
                    M= J(L).IndexOf(UnwantedChars.Substring(K,1))
                    ' Was this unwanted character found in this value?
                    If M<>-1 Then
                        ' Yes - where was it found in the value?
                        Select Case M
                            Case 0  ' Beginning of value
                                J(L) = J(L).Substring(1)
                            Case J(L).Length    ' End of value.
                                J(L) = J(L).Substring(0,(M-1))
                            Case Else   ' Somewhere in-between.
                                J(L) = J(L).Substring(0,M) & J(L).Substring(M+1)
                        End Select
                    Else
                        ' No the unwanted character was not found in this value.
                    End If
                Loop Until M=-1 ' Go see if there are more of this unwanted character in the value.
            Next L  ' Next value.
        Next K  ' Next unwanted character.
    
        ' Loop through all the values and trip spaces from beginning and end of each.
        For L As Integer = J.GetLowerBound(0) To J.GetUpperBound(0)
            J(L) = J(L).Trim
        Next L  
    
        ' Assign the J array to the combobox.
        ComboBox1.DataSource = J
    
    End Sub
    
    End Class