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?
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.
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