我知道可以匹配一个单词,然后用其他工具逆转比赛(例如 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
其他回答
这就是我这样做的方式:
^[^h]*(h(?!ede)[^h]*)*$
比其他答案更准确、更有效,它实施了弗里德尔的“无滚动”效率技术,需要更少的后续追踪。
另一个选项是要添加一个积极的视图,并检查视图是否在输入线的任何地方,然后我们会否认,与类似的表达:
^(?!(?=.*\bhede\b)).*$
用词界限。
这个表达式在 regex101.com 的右上方面板上解释,如果你想探索/简化/修改它,在此链接中,你可以看到它会如何与某些样品输入相匹配,如果你愿意。
雷格斯循环
jx.im 可视为常见的表达式:
此分類上一篇
grep "<Regex for 'doesn't contain hede'>" input
原因在于,没有旗帜,POSIX接口只需要使用基本常规表达式(BRE),这些表达式只是不足以完成这个任务,因为缺乏替代的子表达式。
grep "^\([^h]\|h\(h\|eh\|edh\)*\([^eh]\|e[^dh]\|ed[^eh]\)\)*\(\|h\(h\|eh\|edh\)*\(\|e\|ed\)\)$" input
(与格雷尔和一些额外的优化手工完成)。
egrep "^([^h]|h(h|eh|edh)*([^eh]|e[^dh]|ed[^eh]))*(|h(h|eh|edh)*(|e|ed))$" input
#!/bin/bash
REGEX="^\([^h]\|h\(h\|eh\|edh\)*\([^eh]\|e[^dh]\|ed[^eh]\)\)*\(\|h\(h\|eh\|edh\)*\(\|e\|ed\)\)$"
# First four lines as in OP's testcase.
cat > testinput.txt <<EOF
hoho
hihi
haha
hede
h
he
ah
head
ahead
ahed
aheda
ahede
hhede
hehede
hedhede
hehehehehehedehehe
hedecidedthat
EOF
diff -s -u <(grep -v hede testinput.txt) <(grep "$REGEX" testinput.txt)
Files /dev/fd/63 and /dev/fd/62 are identical
如预期。
对于那些对细节感兴趣的人来说,使用的技术是将与词相匹配的常规表达式转换为终端自动,然后转换自动,将每个接受状态转换为不接受,反之亦然,然后将结果的FA转换为常规表达式。
grep -P '^((?!hede).)*$' input
^([^h]|h(h|e(h|dh))*([^eh]|e([^dh]|d[^eh])))*(h(h|e(h|dh))*(ed?)?)?$
我想添加另一个例子,如果你试图匹配一个包含X线的整个线,但也不包含Y线。
这个 regex 模式会工作(在 JavaScript 中也工作)
^(?=.*?tasty-treats)((?!chocolate).)*$
(全球,多线旗在例子中)
互动示例: https://regexr.com/53gv4
比赛
(这些 URL 包含“蛋糕治疗”并且不包含“巧克力”)
example.com/tasty-treats/strawberry-ice-cream example.com/甜点/tasty-treats/banana-pudding example.com/tasty-treats-overview
没有匹配
example.com/tasty-treats/chocolate-cake example.com/home-cooking/over-roasted-chicken example.com/tasty-treats/banana-chocolate-fudge example.com/desserts/chocolate/tasty-treats example.com/chocolate/tasty-treats/desserts
我能找到的最简单的事情就是
[^(hede)]
在 https://regex101.com/ 上测试
您也可以在该网站上添加单元测试案例