0

我正在寻找某种方法来摆脱方法中的collection对象collect_data并直接返回匹配的数据。

def collect_data(string)
  collection = []
  string.gsub(/a\=\[(.+?)\](\s|$)/) { collection << $1 }
  collection
end

一些例子它应该如何工作

string = "a=[111] b=[555] a=[123]"

collect_data(string) # =>  ["111", "123"]
4

1 回答 1

2
"a=[111] b=[555] a=[123]".scan(/a\=\[(.+?)\](\s|$)/).map(&:first)
于 2012-07-13T11:20:06.543 回答