正则表达式前瞻grep中的“未跟随" [英] Regex lookahead for 'not followed by' in grep
问题描述
我试图对 Ui.
的所有实例进行 grep,后面没有 Line
甚至只是字母 L
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?
使用前瞻
grep "Ui.(?!L)" *
bash: !L: event not found
grep "Ui.(?!(Line))" *
nothing
推荐答案
负前瞻,这是您所追求的,需要比标准 grep
更强大的工具.您需要启用 PCRE 的 grep.
Negative lookahead, which is what you're after, requires a more powerful tool than the standard grep
. You need a PCRE-enabled grep.
如果你有 GNU grep
,当前版本支持选项 -P
或 --perl-regexp
然后你可以使用正则表达式你想要的.
If you have GNU grep
, the current version supports options -P
or --perl-regexp
and you can then use the regex you wanted.
如果您没有(足够新的版本)GNU grep
,请考虑获取 <代码>确认代码>.
If you don't have (a sufficiently recent version of) GNU grep
, then consider getting ack
.
这篇关于正则表达式前瞻grep中的“未跟随"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!