替换字符串中的第二个 integer/number

Replace the second integer/number in a string

给定不同的字符串和每两个整数,Ruby (2.7) 中替换第二个整数的最简单方法是什么?

示例:

'We are there from 2 to 5 oclock'
'You can stack 15 onto 22 boxes'

到目前为止我正在使用 string::gsup 但我想知道是否有更好的方法?

gsub 是完成这项工作的最简单方法。

输入

a='We are there from 2 to 5 oclock'

代码

p a.gsub(/\D*\d+\D+\K\d+/,"10000")

输出

"We are there from 2 to 10000 oclock"