grep -o '<img[^>]*src="[^"]*photobucket[^>]*>' infile
-o
仅返回匹配项。分开:
<img # Start with <img
[^>]* # Zero or more of "not >"
src=" # start of src attribute
[^"]* # Zero or more or "not quotes"
photobucket # Match photobucket
[^>]* # Zero or more of "not >"
> # Closing angle bracket
对于输入文件
<img src="/imgs/test.jpg">
<img src="/imgs/thiswillgetpulledtoo.jpg"><p>We like photobucket</p>
<img src="/photobucket/img21.png">
<img alt="photobucket" src="/something/img21.png">
<img alt="something" src="/photobucket/img21.png">
<img src="/photobucket/img21.png" alt="something">
<img src="/something/img21.png" alt="photobucket">
这返回
$ grep -o '<img[^>]*src="[^"]*photobucket[^>]*>' infile
<img src="/photobucket/img21.png">
<img alt="something" src="/photobucket/img21.png">
<img src="/photobucket/img21.png" alt="something">
非贪婪.*?
仅适用于-P
选项(Perl 正则表达式)。