Regex.splitpath

Regex.splitpath,regex,vb.net,Regex,Vb.net,我必须将一个没有文件名的路径拆分到peaces。 由于路径的来源可能来自不同的操作系统,我认为最好使用Regex 路径的示例可以是: 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 =

我必须将一个没有文件名的路径拆分到peaces。 由于路径的来源可能来自不同的操作系统,我认为最好使用Regex

路径的示例可以是:

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

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

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

你要找的正则表达式是“谢谢你,狼”。如果是尾随(最后一个)斜杠,此表达式将给出最后一个空字符串。这个能修好吗?其他情况下,这很好解决!你可以用“答案”的形式回答我,这样我就可以投赞成票。我认为最好使用安东在下面的答案中提出的方法。当你需要手动或使用不带Split()方法的语言执行此类拆分时,我会执行以下操作:(1)用标准分隔符替换所有非标准分隔符(2)添加额外的第一个和最后一个分隔符(3)替换所有出现的双/三/etc(4),然后使用正则表达式或charindex,无论该语言有什么方法