Grep正则表达式只能选择10个字符

编程入门 行业动态 更新时间:2024-10-28 14:37:03
本文介绍了Grep正则表达式只能选择10个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

日志文件的模式看起来像这样

一些随机字符串....发送的消息[10个字符 -

我只对 [ 和 -

)之间存在的10个字符串感兴趣

我已经成功地设计了这个看起来像这样的正则表达式。

Message Sent \ [ 。\ {10 \} -

我已经通过 here 。 但不幸的是,正则表达式不会产生任何输出

grep -oMessage Sent \ [.\ {10 \}) - log / development.log

但我最感兴趣的是10个字符,但是上面的正则表达式产量'Message Sent [' + 10个字符 + -

任何线索?

解决方案

使用 \K (来自Perl-regex的模式 )放弃先前匹配的字符。 $ b

grep -oP'Message Sent \ [\K。{10}(?= - +)'log / development.log

grep -oP'Message \s + Sent\s + \ [\K。{10}(?= \s + - +)'log / development.log

DEMO

I have a log file that I'm grep(ing) to select certain character.

The pattern of the log file looks like this

Some Random string.... Message Sent [10 character --

What I'm interested in only the 10 character string present between [ and --

I have successfully managed to compose the regex that look like this

"Message Sent \[.\{10\} --"

I have cross checked the above regex over here.

But unfortunately the regex does not yield any output

grep -o "Message Sent \[.\{10\}) --" log/development.log

But I'm mostly interested in the 10 character but the above regex yield 'Message Sent [' + 10 character + --

Any Clue ?

解决方案

Use \K (pattern from Perl-regex) to discard the previously matched chars.

grep -oP 'Message Sent \[\K.{10}(?= -+)' log/development.log

or

grep -oP 'Message\s+Sent\s+\[\K.{10}(?=\s+-+)' log/development.log

DEMO

更多推荐

Grep正则表达式只能选择10个字符

本文发布于:2023-11-10 23:05:31,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   正则表达式   Grep

发布评论

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

>www.elefans.com

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