我有一个文件名数组。这些中的一部分可能具有类似这样的模式(字母串在末尾带有数字):
arr = %w[
WordWord1.html
WordWord3.html
WordWord10.html
WordWord11.html
AnotherWord1.html
AnotherWord2.html
FileFile.html
]
如何识别相似的子串(它们具有相同的子串,只是它们的数字不同)并将它们移动到数组中?
['WordWord1.html', 'WordWord3.html', 'WordWord10.html', 'WordWord11.html']
['AnotherWord1.html', 'AnotherWord2.html']
['FileFile.html']
答案 0 :(得分:6)
arr.group_by { |x| x[/[a-zA-Z]+/] }.values
答案 1 :(得分:3)
filenames = ["WordWord1.html", "WordWord3.html", "WordWord10.html", "WordWord11.html", "AnotherWord1.html", "AnotherWord2.html", "FileFile.html"]
filenames.inject({}){|h,f|k = f.split(/[^a-zA-Z]/, 2).first;h[k] ||= [];h[k] << f; h}
答案 2 :(得分:2)
arr = %w[
WordWord1.html
WordWord3.html
WordWord10.html
WordWord11.html
AnotherWord1.html
AnotherWord2.html
FileFile.html
]
result = {}
arr.each do |a|
prefix = a.match(/[A-Za-z]+/).to_s
if result[prefix]
result[prefix] << a
else
result[prefix] = [a]
end
end
p result