![](/img/trans.png)
[英]Regular expression in .net to exclude a particular word and multiple whitespaces and line breaks
[英]Regular expression for filenames that doesn't exclude whitespaces
我一直在使用此正则表达式从文件路径字符串中提取文件名:
Regex r = new Regex(@"\w+[.]\w+$+");
只要文件名中没有空格,此方法就起作用。 例如:
r.Match("c:\somestuff\myfile.doc").Value = "myfile.doc"
r.Match("c:\somestuff\my file.doc").Value = "file.doc"
我需要用正则表达式给我“我的file.doc”,而不仅仅是“ file.doc”
我尝试自己弄弄这个表达。 特别是在了解到匹配空格后,我尝试添加\\ s +。 我没有得到希望的结果。
我确实设计了一种解决方案来完成工作:我从字符串的末尾开始,向后退直到到达反斜杠为止。 这给了我文件名以相反的顺序(即cod.elifym)到一个char数组中,然后我使用Array.Reverse()来解决它。 但是,我想学习如何通过简单地修改原始正则表达式来实现这一目标。
它必须是正则表达式吗? 请改用System.IO.Path.GetFileName() 。
Regex r = new Regex(@"[\w ]+\.\w+$");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.