小编Nul*_*Ref的帖子

Ruby:将字符串中的所有整数递增+1

我正在寻找一种简洁的方法来将字符串中的所有整数递增+1并返回完整的字符串.

例如:

"1 plus 2 and 10 and 100"
Run Code Online (Sandbox Code Playgroud)

需要成为

"2 plus 3 and 11 and 101"
Run Code Online (Sandbox Code Playgroud)

我可以很容易地找到所有整数

"1 plus 2 and 10 and 100".scan(/\d+/)
Run Code Online (Sandbox Code Playgroud)

但是我在这一点上陷入困​​境,试图增加并将部件重新组合在一起.

提前致谢.

ruby regex string

5
推荐指数
2
解决办法
1286
查看次数

标签 统计

regex ×1

ruby ×1

string ×1