Search code examples
vb.netstringdirectorystrip

Get text between \ and \PARTS from file name string vb.net


I need to obtain a value of everything between \ & \PARTS

Example:

`XamoutOFdirectorys\THIS IS WHAT I WANT\PARTS`

resulting in the text,  THIS IS WHAT I WANT

in dir1\dir2\THIS IS WHAT I WANT\PARTS

I want the text THIS IS WHAT I WANT and not dir2\THIS IS WHAT I WANT

How can I achive that?

The reason is that I need to know what the files name is that is found before the PARTS directory, regardless of howmany directorys are found before and after...

the closest i can achive is ...

        Dim text As String = "fat\dir1\dir2\PARTS\bat"
    Dim str As String = text.Split("\"c, "\PARTS"c)(1)
    MsgBox(str)

Solution

  •     Dim str As String = "fat\dir1\dir2\PARTS\bat"
        Dim dir As Match = Regex.Match(str, "\\([A-Za-z0-9\-]+)\\PARTS")
        MsgBox(dir.Groups(1).ToString)