【问题标题】:Searching multiple .txt files for all occurrences of a string?在多个 .txt 文件中搜索所有出现的字符串?
【发布时间】:2015-07-12 14:55:34
【问题描述】:

我正在尝试创建一个工具,该工具将搜索 300 多个 .txt 文件中的字符串,该字符串可能在 300 多个 .txt 文件中的每个文件中使用多次

我希望能够遍历每个文件并获取每次出现之间的字符串。

我知道这听起来有点扭曲,我在测试代码时一直摸不着头脑。

我尝试过的

我通读每个文件并检查它是否包含我的搜索文本至少一次,如果包含,那么我将(包含它的文件)的完整路径添加到列表中

Dim FileNamesList As New List(Of String)
    Dim occurList As New List(Of String)

    Dim textSearch As String = TextBox1.Text.ToLower

    'check each file to see if it even contains textbox1.text
    'if it does, then add matching files to list
    For Each f As FileInfo In dir.GetFiles("*.txt")

        Dim tmpRead = File.ReadAllText(f.FullName).ToLower

        Dim tIndex As Integer = tmpRead.IndexOf(textSearch)

        If tIndex > -1 Then
            FileNamesList.Add(f.FullName)

        End If

    Next

然后我想,哦,现在我需要做的就是检查“已批准”文件列表中的每个字符串,并将每个字符串的全部内容添加到一个新列表中。

然后我遍历“那个”列表中的每一个,并获取两个分隔符之间的字符串。

而且...我只是从那里迷路了...

这是我尝试使用的分隔符之间的获取字符串。

  Private Function GetStringBetweenTags(ByVal startIdentifer As String, ByVal endIndentifier As String, ByVal textsource As String) As String
    Dim idLength As Int16 = startIdentifer.Length

    Dim s As String = textsource

    Try

        s = s.Substring(s.IndexOf(startIdentifer) + idLength)
        s = s.Substring(0, s.IndexOf(endIndentifier))
        'MsgBox(s)

    Catch
    End Try
    Return s
End Function

简单来说...

  • 我有 300 个 .txt 文件
  • 有些可能包含我所追求的字符串
  • 我想要每个字符串的子字符串

通常我很好,从不需要问问题,但有太多的强迫症发生。

逻辑示例

== Table.txt ==

print("I am tony")
print("pineapple")
print("brown cows")
log("cable ties")
log("bad ocd")
log("bingo")

== Cherry.txt ==

print("grapes")
print("pie")
print("apples")
log("laugh")
log("tuna")
log("gonuts")

== Tower.txt ==

print("tall")
print("clouds")
print("nomountain")
log("goggles?")
log("kuwait")
log("india")

我想以所有 3 个文件中仅打印功能之间的文本列表结束

没有找到任何关于这个的其他线程,可能是因为它很愚蠢。

所以我应该以

结尾
 ==  ResultList == 

    I am tony
    pineapple
    brown cows
    grapes
    pie
    apples
    tall
    clouds
    nomountain

【问题讨论】:

    标签: regex vb.net string basic


    【解决方案1】:

    RegEx 可能是您处理此类问题的最佳选择。例如:

    Dim results As New List(Of String)()
    Dim r As New RegEx("print\(""(.*)""\)")
    For path As String In filePaths
        Dim contents As String = File.ReadAllText(path)
        For Each m As Match in r.Matches(contents)
            If m.Sucess Then
                results.Add(m.Groups(1).Value)
            End If
        Next
    Next
    

    如您所见,代码循环遍历文件路径列表。对于每一个,它将文件的全部内容加载到一个字符串中。然后它在文件内容字符串中搜索与以下正则表达式模式匹配的所有匹配项:print\("(.*)"\)。然后它遍历所有这些模式匹配并从每个匹配中获取第一个捕获组的值。这些被添加到结果列表中,其中包含您想要的字符串。以下是 RegEx 各部分的含义:

    • print - 查找任何以“print”开头的字符串
    • \( - "print" 后面的下一个字符必须是左括号(反斜杠是转义字符)
    • " - 开括号后的下一个字符必须是双引号字符(重复两次以将其转义以使 VB 不会认为它是字符串的结尾)。
    • (.*) - 括号将其定义为捕获组(以便我们可以从匹配项中仅提取此值)。 .* 表示任意长度的任意字符。
    • "\) - 匹配的字符串必须以双引号结尾,后跟右括号。

    【讨论】:

    • 非常感谢,史蒂文,也感谢您对运营商的澄清。我之前实际上使用过Regex,但这次我没有想到它。我想我应该继续编码以坚持下去。 8个月的懒惰,无济于事。 :)
    【解决方案2】:

    使用正则表达式:

    Imports System.Text.RegularExpressions
    Module Module1
    
        Sub Main()
            Dim input1 As String = _
                "print(""I am tony"") " + _
                "print(""pineapple"") " + _
                "print(""brown cows"") " + _
                "log(""cable ties"") " + _
                "log(""bad ocd"") " + _
                "log(""bingo"")"
    
            Dim input2 As String = _
                "print(""grapes"") " + _
                "print(""pie"") " + _
                "print(""apples"") " + _
                "log(""laugh"") " + _
                "log(""tuna"") " + _
                "log(""gonuts"")"
    
            Dim input3 As String = _
               "print(""tall"") " + _
               "print(""clouds"") " + _
               "print(""nomountain"") " + _
               "log(""goggles?"") " + _
               "log(""kuwait"") " + _
               "log(""india"")"
    
            Dim pattern As String = "print\(""([^""]*)""\)"
            Dim expr As Regex = New Regex(pattern, RegexOptions.Singleline)
            Dim matches As MatchCollection = Nothing
            Dim data As List(Of String) = New List(Of String)()
    
            matches = expr.Matches(input1)
            For Each mat As Match In matches
                data.Add(mat.Groups(1).Value)
            Next mat
            matches = expr.Matches(input2)
            For Each mat As Match In matches
                data.Add(mat.Groups(1).Value)
            Next mat
            matches = expr.Matches(input3)
            For Each mat As Match In matches
                data.Add(mat.Groups(1).Value)
            Next mat
    
        End Sub
    
    End Module
    

    【讨论】:

    • 我将您的 sn-p 更改为简单的代码块格式,因为 sn-p 功能不支持 Visual Basic。将来可能会添加 VB 支持,但现在仅支持 AFAIK 的 JavaScript。
    • 感谢两位的支持,这将有很大帮助! :)
    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2011-04-28
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多