正则表达式在grep中“未遵循” [英] Regex lookahead for 'not followed by' in grep
问题描述
我试图grep所有 Ui \。
没有跟着行
的实例,甚至只是字母 L
什么是编写正则表达式的正确方法,用于查找特定字符串的所有实例另一个字符串?
使用lookaheads
grepUi \。 (?!L)*
bash:!L:未找到事件
grepUi\。(?!(Line))*
什么都没有
,需要比标准 grep
更强大的工具。如果您有GNU grep
,则当前版本支持<$ c>选项。
- perl-regexp
然后你就可以使用你想要的正则表达式了。 如果您没有GNU grep
的最新版本,请考虑获取 ack
。
I am attempting to grep for all instances of Ui\.
not followed by Line
or even just the letter L
What is the proper way to write a regex for finding all instances of a particular string NOT followed by another string?
Using lookaheads
grep "Ui\.(?!L)" *
bash: !L: event not found
grep "Ui\.(?!(Line))" *
nothing
Negative lookahead, which is what you're after, requires a more powerful tool than the standard grep
. You need a PCRE-enabled grep.
If you have GNU grep
, the current version supports options -P
or --perl-regexp
and you can then use the regex you wanted.
If you don't have (a sufficiently recent version of) GNU grep
, then consider getting ack
.
这篇关于正则表达式在grep中“未遵循”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!