如何在没有 Lookbehind 支持的情况下查找单词的特定实例
How to find specific instance of a word without Lookbehind support
https://regex101.com/r/55DgSB/2
我需要在这个 7-Zip 输出中找到 3 个属性的值,'Path ='、'Size =' 和 'Modified ='。我正在使用 VBScript.RegExp,所以(正面)不支持后视。我正在努力处理 'Path =' 一个,因为那个在里面有两次,我需要有第二个实例(10 个破折号之后的那个)。
^((?<=-{10}\n)Path = |^Size = |^Modified = ).*
上面显然不起作用,因为它使用 Lookbehind 检查 10 个破折号。
如何解决?
使用非捕获组设置左侧上下文并使用捕获组获取所需结果:
(?:-{10}\r?\nPath = |^Size = |^Modified = )(.*)
^--------- non-capturing group -----------^
^--^ - capturing group
参见regex demo。
VBA 演示:
Dim re, testString, colMatch, objMatch
Set re = New RegExp
With re
.Pattern = "(?:-{10}\r?\nPath = |^Size = |^Modified = )(.*)"
.Global = True
.Multiline = True
.IgnoreCase = True
End With
testString = "----------" & vbCrLf & "Path = some/path/here"
Set colMatch = re.Execute(testString)
For Each objMatch In colMatch
Debug.Print objMatch.SubMatches(0) ' <- The first submatch is your value
Next
https://regex101.com/r/55DgSB/2
我需要在这个 7-Zip 输出中找到 3 个属性的值,'Path ='、'Size =' 和 'Modified ='。我正在使用 VBScript.RegExp,所以(正面)不支持后视。我正在努力处理 'Path =' 一个,因为那个在里面有两次,我需要有第二个实例(10 个破折号之后的那个)。
^((?<=-{10}\n)Path = |^Size = |^Modified = ).*
上面显然不起作用,因为它使用 Lookbehind 检查 10 个破折号。 如何解决?
使用非捕获组设置左侧上下文并使用捕获组获取所需结果:
(?:-{10}\r?\nPath = |^Size = |^Modified = )(.*)
^--------- non-capturing group -----------^
^--^ - capturing group
参见regex demo。
VBA 演示:
Dim re, testString, colMatch, objMatch
Set re = New RegExp
With re
.Pattern = "(?:-{10}\r?\nPath = |^Size = |^Modified = )(.*)"
.Global = True
.Multiline = True
.IgnoreCase = True
End With
testString = "----------" & vbCrLf & "Path = some/path/here"
Set colMatch = re.Execute(testString)
For Each objMatch In colMatch
Debug.Print objMatch.SubMatches(0) ' <- The first submatch is your value
Next