[英]Simple C# regex
I have a regex I need to match against a path like so: " C:\\Documents and Settings\\User\\My Documents\\ScanSnap\\382893.pd~
". 我有一个正则表达式,我需要针对这样的路径进行匹配:“
C:\\Documents and Settings\\User\\My Documents\\ScanSnap\\382893.pd~
”。 I need a regex that matches all paths except those ending in '~' or ' .dat
'. 我需要一个正则表达式来匹配所有以'〜'或'
.dat
'结尾的路径。 The problem I am having is that I don't understand how to match and negate the exact string ' .dat
' and only at the end of the path. 我遇到的问题是我不理解如何仅在路径的末尾匹配和否定精确的字符串'
.dat
'。 ie I don't want to match {d,a,t}
elsewhere in the path. 即我不想在路径中的其他地方匹配
{d,a,t}
。
I have built the regex, but need to not match .dat 我已经建立了正则表达式,但是不需要匹配.dat
[\\w\\s:\\.\\\\]*[^~]$[^\\.dat]
[\\w\\s:\\.\\\\]*
This matches all words, whitespace, the colon, periods, and backspaces. [\\w\\s:\\.\\\\]*
这匹配所有单词,空格,冒号,句号和退格键。 [^~]$[^\\.dat]$
This causes matches ending in '~' to fail. [^~]$[^\\.dat]$
这会导致以'〜'结尾的匹配失败。 It seems that I should be able to follow up with a negated match for '.dat', but the match fails in my regex tester. 看来我应该可以对'.dat'进行否定的匹配,但是该匹配在我的正则表达式测试器中失败。
I think my answer lies in grouping judging from what I've read, would someone point me in the right direction? 我认为我的答案在于根据我所阅读的内容进行分组,有人会指出我正确的方向吗? I should add, I am using a file watching program that allows regex matching, I have only one line to specify the regex.
我应该补充一点,我正在使用一个允许正则表达式匹配的文件监视程序,我只有一行可以指定正则表达式。
This entry seems similar: Regex to match multiple strings 此项看起来类似:正则表达式以匹配多个字符串
You want to use a negative look-ahead : 您想使用否定的前瞻 :
^((?!\.dat$)[\w\s:\.\\])*$
By the way, your character group ( [\\w\\s:\\.\\\\]
) doesn't allow a tilde (~) in it. 顺便说一句,您的字符组(
[\\w\\s:\\.\\\\]
)不允许在其中使用波浪号(〜)。 Did you intend to allow a tilde in the filename if it wasn't at the end? 您是否打算在文件名中不允许波浪号? If so:
如果是这样的话:
^((?!~$|\.dat$)[\w\s:\.\\~])*$
The following regex: 以下正则表达式:
^.*(?<!\.dat|~)$
matches any string that does NOT end with a '~' or with '.dat'. 匹配不以“〜”或“ .dat”结尾的任何字符串。
^ # the start of the string
.* # gobble up the entire string (without line terminators!)
(?<!\.dat|~) # looking back, there should not be '.dat' or '~'
$ # the end of the string
In plain English: match a string only when looking behind from the end of the string, there is no sub-string '.dat' or '~' . 用简单的英语来说: 仅当从字符串末尾向后看时才匹配字符串,没有子字符串'.dat'或'〜' 。
Edit: the reason why your attempt failed is because a negated character class, [^...] will just negate a single character. 编辑:您的尝试失败的原因是因为否定的字符类[^ ...]将否定单个字符。 A character class always matches a single character.
字符类始终与单个字符匹配。 So when you do [^.dat], you're not negating the string ".dat" but you're matching a single character other than '.', 'd', 'a' or 't'.
因此,当您执行[^ .dat]时,并不是在否定字符串“ .dat”,而是要匹配'。','d','a'或't'以外的单个字符。
^((?!\.dat$)[\w\s:\.\\])*$
This is just a comment on an earlier answer suggestion: 这只是对较早答案的评论:
. 。 within a character class, [], is a literal .
在字符类[]中,是一个文字。 and does not need escaping.
并且不需要转义。
^((?!\.dat$)[\w\s:.\\])*$
I'm sorry to post this as a new solution, but I apparently don't have enough credibility to simply comment on an answer yet. 很抱歉将其作为新的解决方案发布,但是我显然没有足够的信誉来仅对答案发表评论。
I believe you are looking for this: 我相信您正在寻找:
[\w\s:\.\\]*([^~]|[^\.dat])$
which finds, like before, all word chars, white space, periods (.), back slashes. 像以前一样找到所有字符char,空格,句点(。),反斜杠。 Then matches for either tilde (~) or '.dat' at the end of the string.
然后在字符串末尾匹配代字号(〜)或'.dat'。 You may also want to add a caret (^) at the very beginning if you know that the string should be at the beginning of a new line.
如果您知道字符串应该在新行的开头,则可能还需要在开始处添加一个插入符号(^)。
^[\w\s:\.\\]*([^~]|[^\.dat])$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.