我有一个大型设备配置文件,我正在尝试使用 RegEx 解析出相关部分以进行进一步编码......我尝试解析的配置部分将以“edit ServiceName ;mode”和将在其自己的行上以“退出”一词结尾。此配置文件和返回的字符串将位于多行。我只想返回或匹配此配置文件中包含某些关键字的某些部分...
Sub TestRegEx_1()
Dim TestString
Dim objRegEx, f_objResults, f_Match
TestString = "edit NonMatch1 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit NonMatch2 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit GoodMatch1 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"KeyWord_1 1 2 and 3" & vbCrLf & _
"exit" & vbCrLf & _
"edit GoodMatch2 ;mode" & vbCrLf & _
"KeyWord_2 A B and C" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit NonMatch3 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit GoodMatch3 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"KeyWord_3 1A" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit"
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
objRegEx.Global = True
objRegEx.Pattern = "^edit (.{0,}) \;mode[\s\S]*?" & _
"(?=(KeyWord_1|KeyWord_2|KeyWord_3))[\s\S]*?exit$"
Set f_objResults = objRegEx.Execute(TestString)
For Each f_Match In f_objResults
MsgBox f_Match.Value
Next
End Sub
因为 RegEx 是贪婪的,所以上面的例程将返回一个包含我不想要的部分的匹配项。我能够将我的例程拆分为两个单独的 RegEx 模式搜索以使其正常运行,但我想修改我的初始模式搜索,这样我就不必这样做了。下面的例程将创建我正在寻找的输出。
Sub TestRegEx_2()
Dim TestString
Dim objRegEx, f_objResults, f_Match
TestString = "edit NonMatch1 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit NonMatch2 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit GoodMatch1 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"KeyWord_1 1 2 and 3" & vbCrLf & _
"exit" & vbCrLf & _
"edit GoodMatch2 ;mode" & vbCrLf & _
"KeyWord_2 A B and C" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit NonMatch3 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit" & vbCrLf & _
"edit GoodMatch3 ;mode" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"KeyWord_3 1A" & vbCrLf & _
"Something Random" & vbCrLf & "Something Random" & vbCrLf & _
"exit"
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
objRegEx.Global = True
'This Works...
objRegEx.Pattern = "^edit (.{0,}) \;mode[\s\S]*?exit$"
Set f_objResults = objRegEx.Execute(TestString)
objRegEx.Pattern = "(?=(KeyWord_1|KeyWord_2|KeyWord_3))"
For Each f_Match In f_objResults
If objRegEx.test(f_Match.Value) Then
MsgBox f_Match.Value
End If
Next
End Sub
我需要对我的初始模式匹配进行哪些更改才能使其工作而无需创建单独的 RegEx 模式?如何明确告诉 RegEx 引擎在“exit”的第一个实例之后停止,以便如果它没有找到匹配项,它不会继续包含其他字符串,直到找到匹配项?任何帮助是极大的赞赏!谢谢你。
编辑:添加了我希望匹配返回的测试字符串中的部分。“GoodMatch”部分可以包含一个或多个关键字。我需要返回完整的部分。
edit GoodMatch1 ;mode
Something Random
Something Random
KeyWord_1 1 2 and 3
exit
edit GoodMatch2 ;mode
KeyWord_2 A B and C
Something Random
Something Random
exit
edit GoodMatch3 ;mode
Something Random
Something Random
KeyWord_3 1A
Something Random
Something Random
exit