Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use non-capturing group in VBA regex?

Tags:

regex

vba

With VBA, I'm trying to use regex to capture the filename from a UNC path without the extension--looking at .TIF files only.

So far this is what I have:

Function findTIFname(filestr As String) As String

Dim re As RegExp
Dim output As String
Dim matches As MatchCollection

Set re = New RegExp
re.pattern = "[^\\]+(?:[.]tif)$"
Set matches = re.Execute(filestr)
If matches.Count > 0 Then
    output = matches(0).Value
Else
    output = ""
End If
findTIFname = output

End Function

But when I run the function as follows:

msgbox findTIFname("\\abc\def\ghi\jkl\41e07.tif")

I get the following output:

41e07.tif

I thought that "(?:xxx)" was the regex syntax for a non-capturing group; what am I doing wrong?

like image 407
sigil Avatar asked Dec 27 '22 16:12

sigil


1 Answers

The syntax (?:...) is a non-capturing group. What you need here is a positive lookahead assertion which has a (?=...) syntax like so:

re.pattern = "[^\\]+(?=[.]tif$)"

Note that lookaround assertions have zero width and consume no characters.

like image 105
ridgerunner Avatar answered Dec 30 '22 11:12

ridgerunner