正/负先行使用grep和perl [英] Positive/Negative lookahead with grep and perl

查看:168
本文介绍了正/负先行使用grep和perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的login.txt文件包含以下条目

my login.txt file contains following entries

abc def
abc 123
def abc
abc de
tha ewe

当我用perl做积极的前瞻,我得到以下结果。

when i do the positive lookahead using perl, i'm getting the following result

cat login.txt | perl -ne 'print if /(?)abc\s(?=def)/'
abc def

当我使用grep我得到以下结果。

when i use grep i'm getting the following result

cat login.txt | grep -P '(?<=abc)\s(?=def)'
abc def

在Perl和grep如下负lookahed结果。

negative lookahed results as follows from perl and grep.

 cat login | perl -ne 'print if /(?)abc\s(?!def)/'
abc 123
def abc
abc de

grep的结果。

grep result

cat login.txt | grep -P '(?<=abc)\s(?!def)'
abc 123
abc de

perl的匹配为负先行的DEF ABC。但它不应该高清匹配ABC,因为我检查ABC然后高清模式。 grep的返回正确的结果。

perl matched the def abc for the negative lookahead. but it shouldn't matched def abc, as i'm checking abc then def pattern. grep returning the correct result.

时,东西在我的Perl模式不见了?

is something missing in my perl pattern ?

推荐答案

的grep不包括它会检查对正则表达式的字符串中的换行,所以 ABC \\ S 做当美国广播公司是在该行的末尾不匹配。的Chomp Perl或使用-l命令行选项,你会看到类似的结果。

grep does not include the newline in the string it checks against the regex, so abc\s does not match when abc is at the end of the line. chomp in perl or use the -l command line option and you will see similar results.

我不知道为什么你做perl的和grep的正则表达式的其他变化;究竟是什么(?)应该完成?

I'm not sure why you were making other changes between the perl and grep regexes; what was the (?) supposed to accomplish?

这篇关于正/负先行使用grep和perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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