I'm running VBA (Excel 2003) and testing a positive lookbehind regex pattern. I run the function below but get the following error:
Run-time error '5017': Method 'Execute' of object 'IRegExp2' failed
I've also tried
Set re = CreateObject("vbscrip.regexp")
but I get the same error. I've successfully tested some positive lookaheads and they work. It's just the lookbehind that is problematic. I've tested the patter below with Expresso and it worked fine. Is this a flavor problem peculiar to VBA?
Function regexSearch(pattern As String, source As String) As String
Dim re As RegExp
Dim matches As MatchCollection
Dim match As match
'Create RegEx object
pattern = "(?<=a)b"
source = "cab"
Set re = New RegExp
re.Multiline = False
re.Global = True
re.IgnoreCase = False
re.pattern = pattern
'Execute
Set matches = re.Execute(source)
'Output
For Each match In matches
str = str & match & " "
Next match
regexSearch = str
End Function
It is particular to vbscript (and hence vba).
vbscript doesn't support lookbehind (negative or positive)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With