如何在没有Lookbehind支持的情况下查找单词的特定实例

时间:2019-03-27 09:15:11

标签: regex vba vbscript

https://regex101.com/r/55DgSB/2

我需要在此7压缩输出中找到3个属性的值,即“ Path =”,“ Size =”和“ Modified =”。我正在使用VBScript.RegExp,因此不支持(正)Lookbehind。我正在为“ Path =”而苦苦挣扎,因为其中一个在其中两次,并且我需要第二个实例(一个在十个破折号之后的实例)。

^((?<=-{10}\n)Path = |^Size = |^Modified = ).*

以上显然不起作用,因为它正在使用Lookbehind检查10个破折号。 该怎么解决?

1 个答案:

答案 0 :(得分:2)

使用非捕获组来设置左侧上下文,并使用捕获组来获取所需的结果:

(?:-{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