Quote:
Originally Posted by gmaxey
I learned (after some frustration) that look behind assertions are unfortunately not supported in VBA. So, how do achieve the same results with VBA? Here is a look ahead that comes close, but unlike the look behind described above, it returns both parts y and x.
|
As far as I remember, in VBA you can only use look ahead, you can't use look behind. I tested in Excel some time ago. Maybe in Perl, Python, but not in VBA. That's why I didn't mention look behind in the last post
Since you can't use look behind, you have to experiment. In this example, you can e.g.
Code:
Sub RegEx()
Dim RegEx As Object, Matches As Object, Match As Object
Dim oRng As Range
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Global = True
.Pattern = "(?=Tom|Jack)\S+ (Sprat)"
End With
Set Matches = RegEx.Execute(ActiveDocument.Range.Text)
For Each Match In Matches
ActiveDocument.Range(Match.FirstIndex + Match.Length - Len(Match.SubMatches(0)), Match.FirstIndex + Match.Length).HighlightColorIndex = wdBrightGreen
Debug.Print Match.SubMatches(0)
Next
lbl_Exit:
Exit Sub
End Sub