Regex.Split Path

时间:2013-10-31 10:57:35

标签: regex vb.net

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

Path的例子可以是:

     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代码?

1 个答案:

答案 0 :(得分:2)

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

Dim Splitted As String() = _path.Split(New [Char]() {"\"c, "/"c},  StringSplitOptions.RemoveEmptyEntries)