用grep查找两个字符之间的字符串 [英] Find a string between two characters with grep
本文介绍了用grep查找两个字符之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在此 answer 正则表达式中找到了两个字符之间的字符串.就我而言,我想找到‘
和’
之间的所有模式.这是正则表达式:
I have found on this answer the regex to find a string between two characters. In my case I want to find every pattern between ‘
and ’
. Here's the regex :
(?<=‘)(.*?)(?=’)
实际上,当我在 https://regex101.com/上尝试时,它可以工作.
Indeed, it works when I try it on https://regex101.com/.
我要在 grep
中使用它,但是它不起作用:
The thing is I want to use it with grep
but it doesn't work :
grep -E '(?<=‘)(.*?)(?=’)' file
有什么遗漏吗?
推荐答案
这些都是积极的前瞻性,并且在声明后面.您需要使用PCRE(Perl兼容正则表达式)启用它,也许最好使用GNU grep
中的 -o
选项获得匹配的部分:
Those are positive look-ahead and look behind assertions. You need to enable it using PCRE(Perl Compatible Regex) and perhaps its better to get only matching part using -o
option in GNU grep
:
grep -oP '(?<=‘)(.*?)(?=’)' file
这篇关于用grep查找两个字符之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文