比赛只持续指数?${字符串}(3)(5)?使用正则表达式 - match only last index from “${STRING}(3)(5)” using regular expression

- 此内容更新于:2015-12-20
主题:

我有一个字符串“${}字符串(3)(5)“我想只有去年指数使用正则表达式相匹配。像这里应该匹配只有5。怎么办?

原文:

I have a String

"${STRING}(3)(5)"

I want to match only last index using regular expression. like here it should match only 5. how to do?

网友:

(原文:.*\((\d+)\) ....)

网友:

(原文:(\d+)\)$ .....)

网友:@TessellatingHeckler(\d+)):匹配”5美元)”,输出值是5。

(原文:@TessellatingHeckler : (\d+))$ is matching "5)" ,And output value is 5.)

网友:@TessellatingHeckler:你能改正它,只匹配“5”。

(原文:@TessellatingHeckler : could you please correct it , to match only "5" .)

网友:@TessellatingHeckler:也不应该匹配字符串"${}字符串时(2)”或“${}字符串”

(原文:@TessellatingHeckler : Also it should not match when String is "${STRING}(2)" or "${STRING}")

解决方案:
一个正则表达式本身不符合我们想要的。我们将不得不削减字符串直到我们可以匹配一个数字,或我们需要一个工具,它支持反向引用时。从sed利用指向这里有一个例子:也可以用awk将匹配它的头给我们我们想要的结果…
原文:

A regular expression by itself will not match what we want. We will have to whittle down the string until we can get a match on a number, or we need a tool that supports back-references. Here is an example from sed leveraging a back-reference:

sed -e 's:.*\([0-9]+\))$:\1:'

It is also possible with awk to turn the match on its head to give us the result we want...

awk -F '[^0-9]' '{ print $(NF-1) }'