Positive/Negative lookahead with grep and perl

被刻印的时光 ゝ 提交于 2019-12-04 00:27:15

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.

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

I would try anchoring your regex like so:

/(^abc\s+(?!def).+)/

This would capture:

abc 123
abc de

The (?) at the beginning of your negative lookahead regex is redundant

In your perl -ne 'print if /(?)abc\s(?!def)/' you asking perl to find abc, then space, then string shouldn't be def. This is successfully matches with def abc, because there is no def after abc here and \s matches with newline.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!