具有前瞻模式的 grep 命令不选择任何内容 [英] grep command with a lookahead pattern does not select anything
问题描述
我试图使用以下 grep 命令:
I was trying to use the following grep command:
grep '(.*)(?=(png|html|jpg|js|css)(?:s*))(png|html|jpg|js|css.*s)' file
文件包含以下内容:
http://manage.bostonglobe.com/GiftTheGlobe/LandingPage.html
https://manage.bostonglobe.com/cs/mc/login.aspx?p1=BGFooter
https://www.bostonglobe.com/bgcs
/newsletters?p1=BGFooter_Newsletters
https://bostonglobe.custhelp.com/app/home?p1=BGFooter
https://bostonglobe.custhelp.com/app/answers/list?p1=BGFooter
/tools/help/stafflist?p1=BGFooter
https://www.bostonglobemedia.com/
https://manage.bostonglobe.com/Order/newspaper/Newspaper.aspx
https://www.facebook.com/globe
https://twitter.com/#!/BostonGlobe
https://plus.google.com/108227564341535363126/about
https://epaper.bostonglobe.com/launch.aspx?pbid=2c60291d-c20c-4780-9829- b3d9a12687cf
http://nieonline.com/bostonglobe/
https://secure.pqarchiver.com/boston-sub/no_default.html?ss=1&url=%2Fboston-sub%2Fadvancedsearch.html
/tools/help/privacy?p1=BGFooter
/tools/help/terms-service?p1=BGFooter
/termsofpurchase?p1=BGFooter
https://www.bostonglobemedia.com/careers
/css/globe-print.css?v=19256I1935
//meter.bostonglobe.com/css/style.css
/css/globe-print.css?v=19256I1935
//cdn.blueconic.net/bostonglobemedia.js
/js/lib/rwd-images.js,lib/respond.min.js,lib/modernizr.custom.min.js,globe- define.js,globe-controller.js?v=19256I1935
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==
/js/lib/jquery.js,lib/lo-dash-custom-2.4.1.js,lib/a9.js,lib/pb.js,dist/ad- init.js,globe-newsletter.js,globe-profile-page.js,dist/globe-topic-nav.js,dist/rakuten.js?v=19256I1935
//dc8xl0ndzn2cb.cloudfront.net/js/bostonglobe/v0/keywee.min.js
出于某种原因,它没有从该文件中选择任何内容,我尝试了不同的标志,但似乎无法弄清楚出了什么问题
For some reason it doesn't select anything from that file, Ive tried different flags but cant seem to figure out whats wrong
推荐答案
您正在将 PCRE 正则表达式与 POSIX BRE 引擎一起使用,该引擎是默认的 grep
引擎.
You are using a PCRE regex with the POSIX BRE engine that is default grep
engine.
要使这些模式起作用,您应该使用 -P
选项(在 GNU grep
中可用):
To make those patterns work, you should use -P
option (available in GNU grep
):
grep -P 'YOUR_PCRE_PATTERN'
^^
要开发和测试 PCRE 模式,通常推荐使用众所周知的 regex101.com.
To develop and test PCRE patterns, a well-known regex101.com is usually recommended.
请注意,在 Mac OS 上,您可以安装GNU grep
通过 brew
.
Note that on Mac OS, you may install GNU grep
via brew
.
这篇关于具有前瞻模式的 grep 命令不选择任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!