4

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?

1
  • Nice code! I use something very similar, but I use a second paramter for the re.pattern so I can just pass in any paramter rather than hard-code it. Just a tip: VBA will automatically make output = "" when you declare it, so there is no need for that Else statement at the end. And instead of using > 0, you can use <> 0 for better optimization (faster to check for inequality). Commented Jul 28, 2011 at 6:49

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This was very helpful to me too - thanks. Now I am confused what a non-capturing group is even for. Would you append and briefly explain?
@Praesagus - No, but even better allow me to steer you to an excellent tuorial on the subject: Lookaround Assertions. This comprehensive website tutorial is an excellent reference for all things regex. Enjoy!
5

Do you really need to do this with RegEx?
Access (or better, MS Office) has built-in ways to do this quite easily without RegEx.

You just need to reference the Microsoft Scripting Runtime (which should be included in every MS Office installation, as far as I know).
Then you can use the FileSystemObject:

Public Function findTIFname(filestr As String) As String

    Dim fso As FileSystemObject
    Set fso = New FileSystemObject

    If fso.GetExtensionName(filestr) = "tif" Then
        findTIFname = fso.GetBaseName(filestr)
    End If

    Set fso = Nothing

End Function

Given your example UNC path \\abc\def\ghi\jkl\41e07.tif, this will return 41e07.

3 Comments

That's cool. I've never used GetBaseName before - this could be helpful in the future. ^^
I didn't say that FSO is more built-in than RegEx. My point is: why use RegEx to get a file name out of a path when there is a class that serves exactly that purpose.
The types in Microsoft Scripting Runtime aren't part of Office, but can be used from any COM-supporting environment, including VBScript, Borland Delphi and .NET languages (though why you wouldn't want to use the .NET Path class is beyond me).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.