简体   繁体   English

如何使用正则表达式(VB.NET)提取文件位置

[英]How to extract file location using Regular Expressions(VB.NET)

I am facing a problem whereby I am given a string that contains a path to a file and the file's name and I only want to extract the path (without the file's name) 我遇到一个问题,即给我一个包含文件路径和文件名的字符串,而我只想提取路径(不包含文件名)

For example, I will receive something like 例如,我将收到类似

C:\\Users\\OopsD\\Projects\\test.acdbd C:\\ Users \\ OopsD \\ Projects \\ test.acdbd

and from that string I want to extract only 从那串我只想提取

C:\\Users\\OopsD\\Projects C:\\ Users \\ OopsD \\ Projects

I was trying to create a RegEx to match a backslash followed by a word, followed by a dot followed by another word - this is to match the 我试图创建一个RegEx来匹配一个反斜杠,然后是一个单词,然后是一个圆点,再匹配另一个单词-这是为了匹配

\\test.acdbd \\ test.acdbd

part and replace it with empty string so that the final result is 并将其替换为空字符串,以便最终结果为

C:\\Users\\OopsD\\Projects C:\\ Users \\ OopsD \\ Projects

Can anyone, familiar with RegEx, help me on this one? 熟悉RegEx的任何人都可以在这一方面帮助我吗? Also, I will be using regular expressions quite a lot in the future. 另外,将来我会大量使用正则表达式。 Is there a (free) program I can download to create regular expressions? 我可以下载一个(免费)程序来创建正则表达式吗?

The regex that you are looking for is as below: 您正在寻找的正则表达式如下:

[^/]+$

where, ^ (caret): Matches at the start of the string the regex pattern is applied to. 其中, ^ (caret):在应用正则表达式模式的字符串的开头匹配。 Matches a position rather than a character. 匹配位置而不是字符。 Most regex flavors have an option to make the caret match after line breaks (ie at the start of a line in a file) as well. 多数正则表达式都可以选择在换行符之后(即,在文件中一行的开头)使插入符号匹配。

$ (dollar): Matches at the end of the string the regex pattern is applied to. $ (dollar):匹配应用正则表达式模式的字符串的末尾。 Matches a position rather than a character. 匹配位置而不是字符。 Most regex flavors have an option to make the dollar match before line breaks (ie at the end of a line in a file) as well. 大多数正则表达式版本也可以选择在换行符之前(即,文件中的行尾)使美元匹配。 Also matches before the very last line break if the string ends with a line break. 如果字符串以换行符结尾,则也匹配最后一个换行符。

+ (plus): Repeats the previous item once or more. + (plus):一次或多次重复上一个项目。 Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once. 贪婪,因此在尝试对前一项进行较少匹配的置换之前,将匹配尽可能多的项,直到前一项仅被匹配一次。

More reference can be found out at this link. 链接上可以找到更多参考。

Many Regex softwares and tools are out there. 有很多Regex软件和工具。 Some of them are: 他们之中有一些是:

  1. www.gskinner.com/RegExr/ www.gskinner.com/RegExr/
  2. www.txt2re.com www.txt2re.com
  3. Rubular - It is not just for Ruby. Rubular-不仅仅针对Ruby。

Are you really sure you need to be using Regex for such as simple task? 您真的确定需要将Regex用于简单任务吗? How about this: 这个怎么样:

Dim file As New IO.FileInfo(" C:\Users\OopsD\Projects\test.acdbd")
MsgBox(file.Directory.FullName)

Regarding the free program on Regex, I would definitely recommend http://www.gskinner.com/RegExr/ - using it all the time. 关于Regex上的免费程序,我肯定会推荐http://www.gskinner.com/RegExr/-一直使用它。 But you always have to consider alternatives, before going the Regex way. 但是,在使用Regex方式之前,您始终必须考虑替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM