sed regex改为非贪婪替换? [英] sed regex to non-greedy replace?

查看:164
本文介绍了sed regex改为非贪婪替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道另一个问题,该问题非常相似,但是对于由于某些原因我仍然遇到问题.

I am aware of another question that is quite similar, but for some reason I'm still having problems.

我有一个GC日志,我正在尝试修剪包含在[]中的Tenured部分.

I have a GC log that I'm trying to trim out the Tenured section enclosed in [].

63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: [Tenured: 1416K->1065K(1536K), 0.0492621 secs] 1922K->1065K(2112K), 0.0513331 secs]

我申请s/\[Tenured:.*\]//

非常期望,结果会在行的其余部分被贪婪地修剪:

And quite expectantly, the result is trimmed greedily through the remainder of the line:

63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546:

因此,让我们尝试不贪婪地将右括号与s/\[Tenured:[^\]]*\]//匹配,但可惜没有匹配,sed跳过了这一行,产生了相同的原始输出:

So let's try and be non-greedy not match a closing right bracket with s/\[Tenured:[^\]]*\]// but alas no match is made and sed skips over the line, producing the same original output:

63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: [Tenured: 1416K->1065K(1536K), 0.0492621 secs] 1922K->1065K(2112K), 0.0513331 secs]

如何非贪婪地匹配和替换该部分?谢谢,

How do I non-greedily match and replace that section? Thanks,

推荐答案

几乎:s/\ [Tenured:[^]] * \]//

Almost: s/\[Tenured:[^]]*\]//

手册说:

要在列表中包含文字']',请使其成为第一个字符 (在可能的'^'之后).

To include a literal ']' in the list, make it the first character (following a possible '^').

即在这种情况下,不需要反斜杠.

i.e. No backslash is required in this context.

  • Raz

这篇关于sed regex改为非贪婪替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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