查找两个字符之间的字符串

查找两个字符之间的字符串

本文介绍了用 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 Compatible Regex)启用它,也许最好使用 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 查找两个字符之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:38