Regex 在多个.txt文件中搜索字符串的所有匹配项?
我正在尝试创建一个工具来搜索300+.txt文件中的字符串,该字符串可能在300+.txt文件中使用多次 我希望能够遍历每个文件并获得每个事件之间的字符串 这听起来有点扭曲,我知道,在测试代码的时候,我已经挠头好几个小时了 我试过的 我阅读每个文件并至少一次检查它是否包含我的搜索文本,如果包含,则将(包含它的文件)的完整路径添加到列表中Regex 在多个.txt文件中搜索字符串的所有匹配项?,regex,vb.net,string,basic,Regex,Vb.net,String,Basic,我正在尝试创建一个工具来搜索300+.txt文件中的字符串,该字符串可能在300+.txt文件中使用多次 我希望能够遍历每个文件并获得每个事件之间的字符串 这听起来有点扭曲,我知道,在测试代码的时候,我已经挠头好几个小时了 我试过的 我阅读每个文件并至少一次检查它是否包含我的搜索文本,如果包含,则将(包含它的文件)的完整路径添加到列表中 Dim FileNamesList As New List(Of String) Dim occurList As New List(Of String
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
然后我想,哦,现在我需要做的就是检查“已批准”文件列表中的每个字符串,并将每个字符串的全部内容添加到一个新列表中
然后我遍历“that”列表中的每一个,得到两个分隔符之间的字符串
而且。。。我只是从那里迷路了
下面是我尝试使用的分隔符之间的get字符串
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
正则表达式可能是你最好的选择。例如:
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\(“(.*))
。然后,它循环所有这些模式匹配,并从每个模式匹配中获取第一个捕获组的值。这些将添加到结果列表中,其中包含所需的字符串。以下是正则表达式各部分的含义:
-查找任何以“print”开头的字符串print
-单词“print”后的下一个字符必须是开括号(反斜杠是转义字符)\(
-开括号后的下一个字符必须是双引号字符(重复两次以转义,以便VB不会认为它是字符串的结尾)“
-括号将其定义为一个捕获组(以便我们可以从匹配项中仅提取该值)。(.*)
表示任意长度的任何字符*
-匹配字符串必须以双引号结尾,后跟右括号“\)
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
我将您的代码段更改为简单代码块格式,因为该代码段功能不支持Visual Basic。将来可能会添加对VB的支持,但现在只支持AFAIK JavaScript。谢谢你们的支持,这将非常有帮助!:)非常感谢,史蒂文,也感谢您对运营商的澄清。我以前确实用过正则表达式,但这次我没想到。我想我应该继续写代码来保持它。8个月的懒惰无济于事。:)