Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Positive Lookbehind Issue with Excel VBA

Tags:

regex

excel

vba

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
like image 830
TSB Avatar asked Sep 25 '12 02:09

TSB


1 Answers

It is particular to vbscript (and hence vba).

vbscript doesn't support lookbehind (negative or positive)

like image 127
brettdj Avatar answered Oct 25 '22 09:10

brettdj