How can I get a substring from the middle of a file path in VBScript?

纵然是瞬间 提交于 2019-12-12 01:16:55

问题


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.



来源:https://stackoverflow.com/questions/13784445/how-can-i-get-a-substring-from-the-middle-of-a-file-path-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!