我从可能包含整数的不同来源获取任意字符串。以下是一些例子:
"He owns 3 cars"
"John has $23 and 50 cents"
"The range is 1-12 cm"
我想解析它们,以便输出如下:
"3"
"23-50"
"1-12"
答案 0 :(得分:5)
"John has 23$ and 50 cents".scan(/\d+/).join("-") # => "23-50"
"The range is 1-12 cm".scan(/\d+/).join("-") # => "1-12"
"He owns 3 cars".scan(/\d+/).join("-") # => "3"