What's the easiest way to get an equivalent to GNU grep that supports negative lookbehinds?

穿精又带淫゛_ 提交于 2019-12-04 09:17:21

问题


I'm trying to grep through a bunch of files in nested subdirectories to look for regular expression matches; my regex requires negative lookbehind.

Perl has negative lookbehind, but as far as I can tell GNU grep doesn't support negative lookbehinds.

What's the easiest way to get an equivalent to GNU grep that supports negative lookbehinds?

(I guess I could write my own mini-grep in Perl, but that doesn't seem like it should be necessary. My copy of the Perl Cookbook includes source for tcgrep; is that what I should use? If so, where's the latest version? Don't tell me I have to type this entire program!)


回答1:


Use ack! Ack is written in Perl so it uses Perl's regex engine (by default).

The negative look-behind is ack "(?<!bad)boy" (per willert's comment)




回答2:


Thanks to a comment from other question. I've found that negative lookbehind is experimentally supported in grep with the -P/--perl-regexp option, so you may still not need to use a different tool if you prefer to keep using grep.

By the way, my preferred alternative to grep is grin (which is written in python).



来源:https://stackoverflow.com/questions/2490007/whats-the-easiest-way-to-get-an-equivalent-to-gnu-grep-that-supports-negative-l

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