0

我必须将没有文件名的路径拆分为和平。由于路径来源可能来自不同的操作系统,我认为最好使用正则表达式。

路径的示例可以是:

     Dim _path As String = "C:\First\Second\third"
     Dim _path As String = "C:\\First\Second\third/"
     Dim _path As String = "C:/First/Second/third\"
     Dim _path As String = "C:/First\Second\third"
     Dim _path As String = "C://First/Second/third"
     Dim _path As String = "usr/bin/first/second/third"
     Dim _path As String = "/usr/bin/first/second/third/"

...和其他类似的变体。

简而言之,路径必须按照“//”或“\\”或“/”或“\”的顺序拆分

字符串数组的想要结果将是:

    Splitted(0) = "C:"
    Splitted(1) = "First"
    Splitted(2) = "Second"
    Splitted(3) = "Third"

    OR

    Splitted(0) = "usr"
    Splitted(1) = "bin"
    Splitted(2) = "First"
    Splitted(3) = "Second"
    Splitted(4) = "Third"

如何在 VB.NET 中编写那些 Regex.Split 代码?

4

1 回答 1

2

最好和最快的方法是使用 Split 方法而不是 RegExp。

Dim Splitted As String() = _path.Split(New [Char]() {"\"c, "/"c},  StringSplitOptions.RemoveEmptyEntries)
于 2013-10-31T11:22:43.087 回答