Grep不匹配正则表达式 [英] Grep not matching regular expression

查看:126
本文介绍了Grep不匹配正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力地从 grep 中获得匹配的结果.

I'm strugging a bit to get matching results from grep.

给出以下输入文件的内容

Given the following input file's contents

{"foo":29,"bar":30}

{"foo":29, "bar":30}

和以下命令 grep -o'"[^"] +:[0-9] +'input.txt ,我期望有两个匹配的结果.据我所知它支持正则表达式,但是我不明白为什么 grep 不返回任何结果?

and the following command grep -o '"[^"]+":[0-9]+' input.txt, I'm expecting two matching results. As far as I know it supports regular expressions, but I don't understand why grep doesn't return any results?

regexp的其他实现工作正常( https://regex101.com/r/RcONXk/1 ).

Other implementations of regexp are working fine (https://regex101.com/r/RcONXk/1).

推荐答案

您可以尝试一下;

grep -oE '"[^"]+":[0-9]+'

例如:

user@host:/tmp$ echo '{"foo":29, "bar":30}' | grep -oE '"[^"]+":[0-9]+'
"foo":29
"bar":30

man grep:

 -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)

基本与扩展正则表达式

这篇关于Grep不匹配正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆