说我有这样的参考匹配
"some3random5string8"
我希望每个整数后插入空格,所以它看起来像这样
"some3 random5 string8"
我特别想做一个字符串这使用gsub
但我不知道如何访问匹配我的正则表达式的字符。
例如:
temp = "some3random5string8"
temp.gsub(/\d/, ' ') # instead of replacing with a ' ' I want to replace with
# matching number and space
我希望有参考的正则表达式匹配的方式。像$1
所以我可以做一些像temp.gsub(/\d/, "#{$1 }")
(注意,这不起作用)
这可能吗?
我实际上用''some3random5string8”.gsub(/(\ d)/,'\ 1').rstrip'去,因为那样更容易让我明白。谢谢你的伟大答案。 – Dty 2012-03-20 05:24:14