在awk/gawk中匹配正则表达式 [英] matching regex in awk/gawk

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

问题描述

有人可以在awk中使下面的awk行起作用吗?语法使用标准的PCRE regex标准(并且我需要期望一些非数字字符位于第一个数字之前,即字符串看起来像"++3.59 ± 0.04* "). (请注意,我尝试使用[0-9][:digit:]而不是\d)也请注意,我确实阅读了

Can someone make the awk line below work in awk please. The syntax uses the standard PCRE regex standard (and i need to expect that some non-numeric characters are preceded to the first number, that is the string can look like "++3.59 ± 0.04* "). (note that I tried [0-9] and [:digit:] instead of \d) also note that I did read https://www.gnu.org/software/gawk/manual/gawk.html#Regexp

gawk 'BEGIN{test="3.59 ± 0.04";match(test, /^.*?(\d+?\.\d+?)\s*?±\s*?(\d+?\.\d+?)$/, arr);print arr[1];}'

推荐答案

您添加到许多?中,我认为您需要使用[0-9] 同样,当您以^[^0-9]开头时,只会吃掉"非数字字符. 所以总而言之,我认为您想要:

you add to many ? and I think you need to use [0-9] Also when you start with ^[^0-9] only non-numeric characters are "eaten away". so in summary I think you want:

gawk 'BEGIN{test="3.59 ± 0.04";match(test, /^[^0-9.]*([0-9]+\.[0-9]+)\s*±\s*([0-9]+\.[0-9]+)$/, arr);print arr[1];}

将所有非数字字符匹配到第一个数字,然后将第一个数字捕获为组1.

That matches any non-numeric character up to the first numeric one and then captures the first number as group 1.

感谢@Ed Morton的更正.我确实错过了原始正则表达式中第一位数字之后的+.

Thanks @Ed Morton for the corrections. I did miss the + after the first digit in the original regex.

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

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