8 regex
如果我只想匹配fileName
,即
在使用正则表达式C://Directory/FileName.cs
之前,以某种方式忽略了所有内容FileName.cs
.
我该怎么做?
我需要这个用于我正在处理的编译UI ...不能使用编程语言,因为它只接受正则表达式.
有任何想法吗?
Mik*_*scu 14
像这样的东西可能会起作用:
[^/]*$
Run Code Online (Sandbox Code Playgroud)
它将所有字符匹配到不是"/"的行尾.
如果要匹配使用"\"路径分隔符的路径,可以将正则表达式更改为:
[^\]*$
Run Code Online (Sandbox Code Playgroud)
但是,如果您的编程语言或环境需要,请务必避开"\"字符.例如,你可能需要写这样的东西:
[^\\]*$
编辑 我删除了前导"/"和尾随"/",因为它们可能不会引起混淆,因为它们实际上不是regEx的一部分,但它们在表示正则表达式时非常常见.
当然,根据regEx引擎支持的功能,您可以使用前瞻/后视和捕获来制作更好的regEx.
根据您关于需要排除与“abc”不匹配的路径的评论,请尝试以下操作:
^.+/(?:(?!abc)[^/])+$
Run Code Online (Sandbox Code Playgroud)
在正则表达式注释模式下完全拆分出来,即:
(?x) # flag to enable comments
^ # start of line
.+ # match any character (except newline)
# greedily one or more times
/ # a literal slash character
(?: # begin non-capturing group
(?! # begin negative lookahead
# (contents must not appear after the current position)
abc # literal text abc
) # end negative lookahead
[^/] # any character that is not a slash
) # end non-capturing group
+ # repeat the above nc group one or more times
# (essentially, we keep looking for non-backspaces that are not 'abc')
$ # end of line
Run Code Online (Sandbox Code Playgroud)