How can I get a substring from the middle of a fil

2019-08-29 17:50发布

问题:

I have the following string in VBScript:

myPath = "C:\Movies\12 Monkeys\12_MONKEYS.ISO"

The path C:\Movies\ is always going to be the same. So here is another path as an example:

myPath = "C:\Movies\The Avengers\DISC_1.ISO"

My question is, how can I pull only the movie folder name, so in the above examples I would get:

myMovie = "12 Monkeys"
myMovie = "The Avengers"

Is there a way to use RegEx with this? Or should I just do some substring and index calls? What is the easiest way to do this?

回答1:

Consider the code below:

arrPathParts = Split(myPath, "\");
myMovie = arrPathParts(2);

Split the string where the delimiter is the backslash character. Splitting a string returns an array of strings. Your movie is the third item in the array of strings.



回答2:

http://regexr.com?3332n

(?<=C:\\Movies\\).*?(?=\\)

You use assertions so that it finds a string that starts with C:\Movies but does not include it in the results, then a greedy operator to find everything up until the forward slash. You use a look ahead assertion to exclude the forward slash from the results.