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

编程入门 行业动态 更新时间:2024-10-24 12:24:31
本文介绍了用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兼容正则表达式)启用它,也许最好使用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:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   两个   grep

发布评论

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

>www.elefans.com

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