awk gensub遇到了比赛的错误部分 [英] awk gensub got the wrong part of matches

查看:71
本文介绍了awk gensub遇到了比赛的错误部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用awk表达式提取"yy a1 b1"

I want to extract "yy a1 b1" with the awk expression

echo "xx yy [a1 b1] / zz [a2 b2]/" | awk '{p=gensub(/.*\[([a-z0-9 ]+)\].*/,"\\1",1); print $2,p}'

,但总是得到"yy a2 b2".我可能会想出一个解决方案,但只是想知道上面表达的问题. gensub的原型是:

but always get "yy a2 b2". I might work out a solution, but just curious the problem of above expression. The prototype of gensub is:

gensub(regexp, replacement, how [, target])

为什么指定的如何"不生效?

why the specified 'how' not take effect?

推荐答案

要点是,第一个.*字符应尽可能多,从而使正则表达式引擎与其余子模式的最后一个匹配.

The point is that the first .* as many chars as possible, thus making the regex engine match the last occurrence of the remaining subpatterns.

您可以使用以下解决方案:

You may use the following solution:

/[^][]*\[([a-z0-9 ]+)\].*/

其中[^][]*匹配除][之外的任何0个或多个字符,因此,允许正则表达式引擎在第一个[...]

where [^][]* matches any 0 or more chars other than ] and [, thus, allowing the regex engine to stop right before the first [...]

请参见在线演示

这篇关于awk gensub遇到了比赛的错误部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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