正则表达式在grep中“未遵循” [英] Regex lookahead for 'not followed by' in grep

查看:107
本文介绍了正则表达式在grep中“未遵循”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图grep所有 Ui \。没有跟着的实例,甚至只是字母 L



什么是编写正则表达式的正确方法,用于查找特定字符串的所有实例另一个字符串?



使用lookaheads

  grepUi \。 (?!L)* 
bash:!L:未找到事件


grepUi\。(?!(Line))*
什么都没有


解决方案

,需要比标准 grep 更强大的工具。如果您有GNU grep ,则当前版本支持<$ c>选项。

$ c> -P 或者 - 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆