我必須拆分一個沒有文件名的路徑才能實現。 由於路徑的來源可能來自不同的操作系統,我認爲最好的方法是使用正則表達式。路徑Regex.Split 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/"
...等類似的變化。
總之,路徑必須由和以該順序 「//」 或 「\\」 或 「/」 OR 「\」 來splited
串陣列的求購結果將是:
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代碼?
你正在尋找的正則表達式是['[\\\ /] +'](http://refiddle.com/gxv) – GolfWolf
謝謝狼。在尾部(最後一個)斜線的情況下,這個表達式給出最後一個空字符串。這可以修復嗎?其他情況下解決好!您可以以「答覆」的形式給我答覆,以便我可以投票。 –
我認爲在下面的回答中使用Anton提出的方法會更好。 – GolfWolf