我知道可以匹配一个单词,然后用其他工具逆转比赛(例如 grep -v)。但是,可以匹配不包含一个特定的单词,例如 hede,使用常规表达式的线条吗?
入口:
hoho
hihi
haha
hede
代码:
grep "<Regex for 'doesn't contain hede'>" input
所需的产量:
hoho
hihi
haha
我知道可以匹配一个单词,然后用其他工具逆转比赛(例如 grep -v)。但是,可以匹配不包含一个特定的单词,例如 hede,使用常规表达式的线条吗?
入口:
hoho
hihi
haha
hede
代码:
grep "<Regex for 'doesn't contain hede'>" input
所需的产量:
hoho
hihi
haha
当前回答
OP 没有指定或标记帖子,以显示背景(编程语言、编辑器、工具)中将使用 Regex。
对于我来说,有时我需要在使用 Textpad 编辑文件时做到这一点。
Textpad 支持一些 Regex,但不支持 lookahead 或 lookbehind,所以需要几步。
Search string:^(.)
Replace string:<@#-unique-#@>\1
Replace-all
Search string:<@#-unique-#@>.*hede.*\n
Replace string:<nothing>
Replace-all
此分類上一篇
此分類上一篇
Search string:<@#-unique-#@>
Replace string:<nothing>
Replace-all
现在你有原始文本,所有包含字符串的线条已被删除。
此分類上一篇
Search string:^(.)
Replace string:<@#-unique-#@>\1
Replace-all
对于包含字符串标签的所有行,请删除独特的“标签”:
Search string:<@#-unique-#@>(.*hede)
Replace string:\1
Replace-all
此分類上一篇
此时此刻,所有以独特的“标签”开始的线条,不包含线条标签,我现在只能用这些线条来做我的某事。
当我完成时,我将从所有行中删除独特的“标签”(替换行是空的):
Search string:<@#-unique-#@>
Replace string:<nothing>
Replace-all
其他回答
我必须同意其他答案,但是:如果这只是假设问题,那么一个假设不是正确的选择。
使用 ConyEdit,您可以使用命令行 cc.gl!/hede/ 获取不包含 regex 匹配的行,或者使用命令行 cc.dl /hede/ 删除包含 regex 匹配的行。
自推出Ruby-2.4.1以来,我们可以在Ruby的常规表达中使用新缺席运营商。
官方DOC
(?~abc) matches: "", "ab", "aab", "cccc", etc.
It doesn't match: "abc", "aabc", "ccccabc", etc.
因此,在你的情况下 ^(?~hede)$ 为你做工作
2.4.1 :016 > ["hoho", "hihi", "haha", "hede"].select{|s| /^(?~hede)$/.match(s)}
=> ["hoho", "hihi", "haha"]
^((?!hede).) *$ 是一个优雅的解决方案,除非它消耗字符,你将无法将其与其他标准相结合。 例如,说你想检查“hede”的不存在和“haha”的存在。
^(?!.*\bhede\b)(?=.*\bhaha\b)
此前提到的(?(?!)*是很棒的,因为它可以被 anchored。
^(?:(?!hede).)*$ # A line without hede
foo(?:(?!hede).)*bar # foo followed by bar, without hede between them
但在这种情况下,以下几点就足够了:
^(?!.*hede) # A line without hede
此简化已准备好添加“和”条款:
^(?!.*hede)(?=.*foo)(?=.*bar) # A line with foo and bar, but without hede
^(?!.*hede)(?=.*foo).*bar # Same