简体   繁体   English

从字符串中提取 Excel VBA 文件名

[英]Extract Excel VBA filenames from string

I want to extract file names from a string.我想从字符串中提取文件名。 The length of the string and the length of the file name are always different.字符串的长度和文件名的长度总是不同的。 Must be done with VBA!必须用VBA完成!

String:细绳:

href ist gleich: "abc/db://test.pdf|0|100">Bsp.: href ist gleich: "abc/db://test.pdf|0|100">Bsp.:

I would like that: test.pdf我想要那个:test.pdf

I do not know how to proceed.我不知道如何进行。 It would also be nice if the script could extract multiple filenames from a string.如果脚本可以从一个字符串中提取多个文件名,那就太好了。

Zb: Zb:

String:细绳:

href ist gleich: "abc//db://test.t.pdf|0|100" "db://test1.pdf|0|100">Bsp.

I would like that:我想这样:

test.t.pdf test1.pdf

Try this and edit it according to your needs.试试这个并根据您的需要编辑它。 At least it was designed for two of your examples.至少它是为您的两个示例设计的。

Dim sStringToFormat As String
Dim i As Integer
Dim vSplit As Variant
Dim colFileNames As Collection
Dim sFormattedString As String

Set colFileNames = New Collection

sStringToFormat = "href ist gleich: ""abc//db://test.t.pdf|0|100"" ""db://test1.pdf|0|100"">Bsp."

vSplit = Split(sStringToFormat, "/")

For i = LBound(vSplit) To UBound(vSplit)

    If InStr(vSplit(i), ".") > 0 Then
        sFormattedString = Split(vSplit(i), "|")(0)
        sFormattedString = Split(sFormattedString, "<")(0)
        sFormattedString = Split(sFormattedString, ">")(0)
        colFileNames.Add sFormattedString
    End If

Next i
Sub testExtractFileName()
 Debug.Print extractFileName("file://D:/ETVGI_556/Carconfigurator_file/carconf_d.pdf", "//")
 Debug.Print extractFileName("abc//db://test.t.pdf|0|100")
 Debug.Print extractFileName("db://test1.pdf|0|100")
End Sub

Function extractFileName(initString As String, Optional delim As String) As String
 Dim necString As String
   necString = left(initString, InStr(initString, ".pdf") + 3)
   necString = Right(necString, Len(necString) - InStrRev(necString, _
            IIf(delim <> "", delim, "/")) - IIf(delim <> "", Len(delim) - 1, 0))
   extractFileName = necString
End Function

The single condition is that in front of the file name (all the time) to exist "//" characters in the initial string.唯一的条件是在文件名前面(一直)在初始字符串中存在"//"字符。 And of course the file extension to all the time to be .pdf.当然,文件扩展名一直是.pdf. If not, this extension is required and the function can be easily adapted... The function returns full name if the second (optional) parameter will be "//" or just the file name (without path) if it is omitted.如果不是,则需要此扩展名,并且可以轻松调整该函数...如果第二个(可选)参数为"//"则该函数返回全名,如果省略,则仅返回文件名(不带路径)。

One option could be using a pattern where you would match the preceding / and capture in a group 1+ word characters \\w+ followed by .pdf一种选择可能是使用一种模式,您可以在其中匹配前面的/并在一组 1+ 字字符\\w+后跟.pdf捕获

Your value is in capturing group 1.您的价值在于捕获第 1 组。

/(\w+\.pdf)

See a regex demo查看正则表达式演示

If you want to have a broader match than \\w you could extend what you do want to match using a character class or use a negated character class [^ to match any char except the listed in the character class.如果您想要比\\w更广泛的匹配,您可以使用字符类扩展您想要匹配的内容,或使用否定字符类[^来匹配除字符类中列出的字符之外的任何字符。

In this case the negated character class [^/|"\\s] would match any char except / | " or a whitespace character \\s在这种情况下,否定字符类[^/|"\\s]将匹配除/ | "或空白字符\\s之外的任何字符

/([^/|"\s]+\.pdf)

See another regex demo查看另一个正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM