VB.net读取文本文件并使用特定的提取单词填充组合框

时间:2016-01-26 18:34:25

标签: regex vb.net text streamreader

我有一个让我头疼的问题。我真的以为有人会问过这个问题,但是几天的阅读和测试都没有结果。

我有一个开始的文本文件:

"Determining profile based on KDBG search...

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

(两者之间的空白行不是错误,也不是“建议”之前的空格)

我需要阅读仅以“建议...”开头的行,并从“Win”中提取每个唯一的单词,并用它们填充组合框。 (即'WinXPSP2x86'和'WinXPSP3x86')

我知道我需要使用'StreamReader'类并且可能会继续使用正则表达式,但是,作为一个初学者,将它们连接在一起是我目前所不知道的。

有人可以帮忙吗?非常感谢。

2 个答案:

答案 0 :(得分:0)

正如一些人已经建议的那样:

  • 如果文件不是太大,请使用System.IO.File.ReadAllLines
  • 遍历行数
  • 对于每一行,使用Split方法分割空格
  • 检查每个单词的前三个字符

这有效但当然需要一些错误检查等:

        Dim lines() As String = System.IO.File.ReadAllLines("c:\temp\example.txt")

        Dim lineWords() As String
        For Each line As String In lines
            lineWords = line.Split(New Char() {" "}, System.StringSplitOptions.RemoveEmptyEntries)

            For Each word As String In lineWords
                If word.Length > 3 Then
                    If word.Substring(0, 3).ToUpper = "WIN" Then
                        cmbWords.Items.Add(word)
                    End If
                End If                
            Next
        Next

答案 1 :(得分:0)

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