我试图创建一个正则表达式来选择包含空格的文件路径,而不是用引号括起来。 另外,我只想要以卷号开头的路径(例如,C:\,D:\,E :),并且我想忽略路径之后的任何开关或命令。
以下面的列表为例,我用粗体突出显示了我要匹配的所有文本并返回:
C:\此路径包含spaces \ system.sys -switch /命令 C:\ Thispathhasnospaces \文件名.exe \ Sytem32文件\ ThisDidNotBeginWithADriveLetter \ something.doc D:\这个路径也有空格\ something.xlsx “C:\我不在乎它是否用引号\ something.abc包裹”-switch
到目前为止,我提出的是:
^\w:\(.+)(.\w\w\w)
哪种工作,但它选择带有空格和没有空格的路径。 如果路径为四个字符的扩展名,例如.xlsx,它也不会选择完整的文件名
任何帮助将非常感谢。 如果你发布了一个更好的正则表达式,如果你添加了一些解释,那将真的有帮助,因为我正在努力学习它。
谢谢!
I am trying to create a regular expression to select file paths that contain spaces and are not wrapped in quotes. In addition, I only want paths that begin with a volume letter (e.g., C:\, D:\, E:) and I want to ignore any switches or commands that come after the path.
Take for instance the following list, I have highlighted in bold all of the text I want to match and return:
C:\This path has spaces\system.sys -switch /command C:\Thispathhasnospaces\filename.exe \sytem32\ThisDidNotBeginWithADriveLetter\something.doc D:\This path also has spaces\something.xlsx "C:\I don't care if it is wrapped in quotes\something.abc" -switch
So far what I have come up with is:
^\w:\(.+)(.\w\w\w)
Which sort of works, but it selects paths both with spaces and without spaces. It also doesn't select the full filename if the path as a four character extension, such as .xlsx
Any help would be very much appreciated. If you do post a better regex, if you added some explanation it would really help because I am trying to learn it.
Thanks!
最满意答案
我会过去的
^[A-Z]:\\.+\s.+\.\S+ ^是字符串开头的锚点 [AZ]:\\匹配一个字母后跟冒号和反斜杠 .+匹配任何字符,1次或多次 \s匹配单个空格 .+\.\S+匹配任何字符,后跟点和非空格有关演示,请参阅https://regex101.com/r/fC5tF8/2
I would go by
^[A-Z]:\\.+\s.+\.\S+ ^ is an anchor for the start of the string [A-Z]:\\ matches a letter followed by colon and backslash .+ matches any character, 1 or more times \s matches a single space .+\.\S+ matches any characters followed by dot and non-spacesSee https://regex101.com/r/fC5tF8/2 for a demo
更多推荐
发布评论