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

编程入门 行业动态 更新时间:2024-10-24 14:24:40
本文介绍了用 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 :

(?<=‘)(.*?)(?=’)

确实,当我在 regex101/ 上尝试时,它确实有效.

Indeed, it works when I try it on regex101/.

问题是我想将它与 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 查找两个字符之间的字符串

本文发布于:2023-11-02 17:44:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   两个   grep

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!