【发布时间】:2016-04-17 03:42:36
【问题描述】:
代码:
arr = ["this is | a test","this is a second test","this must be a third test"]
res = ["this is | a test test test ","this is a second test test test","this must be a third test test test"]
result = Hash.new
arr.each do |c|
res.each do |i|
if (/#{c}/.match("#{i}" ))
result["#{c}"] = i
end
end
end
puts result
结果:
["this is | a test" => "this is a second test test test",
"this is a second test" => "this is a second test test test",
"this must be a third test" => "this must be a third test test test"]
问题:
第一个匹配错误,“this is | a test”的最后一个匹配是“this is a second test”,我看到他先匹配第一个值,然后匹配第二个值。 插入中断不是一种选择。我怎样才能确定与“|”的匹配完全匹配,所以我可以得到以下结果:
["this is | a test" => "this is | a test test test ",
"this is a second test" => "this is a second test test test",
"this must be a third test" => "this must be a third test test test"]
【问题讨论】:
-
无法复制。
-
为什么不使用反斜杠?
-
@c650 因为数组是动态的,我们永远不知道“|”是否以及何时字符将出现。