Eddict
Eddict

Reputation: 43

How to find specific instance of a word without Lookbehind support

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

I need to find the values of 3 properties, 'Path =', 'Size =' and 'Modified =' inside this 7-Zip output. I'm using VBScript.RegExp so (Positive) Lookbehind is not supported. I'm struggling with the 'Path =' one as that one is in there twice and i need to have the second instance (the one after the 10 dashes).

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

above obviously not working as it's using Lookbehind to check for the 10 dashes. how to solve?

Upvotes: 1

Views: 44

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

Use a non-capturing group to set the left-hand side context and use a capturing group to grab the required result:

(?:-{10}\r?\nPath = |^Size = |^Modified = )(.*)
^--------- non-capturing group -----------^
                                           ^--^ - capturing group

See the regex demo.

VBA demo:

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

Upvotes: 2

Related Questions