使用VBA,我試圖使用正則表達式來捕獲UNC路徑中的文件名,而不使用擴展名 - 僅查看.TIF文件。如何在VBA正則表達式中使用非捕獲組?
到目前爲止,這是我:
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
但是當我運行的功能如下:
msgbox findTIFname("\\abc\def\ghi\jkl\41e07.tif")
我得到以下輸出:
41e07.tif
我想「(?:xxx)」是非捕獲組的正則表達式語法;我究竟做錯了什麼?
不錯的代碼!我使用了非常相似的東西,但我使用了第二個參數來表示重複模式,所以我可以傳入任何參數而不是硬編碼。只是一個提示:當你聲明它時,VBA會自動生成output =「」,所以最後不需要這個Else語句。而不是使用> 0,可以使用<> 0來更好地優化(更快地檢查不等式)。 – aevanko