我想在一个数组上做多个正则表达式替换,我有这个工作代码,但似乎不是ruby方式,谁有更好的解决方案?
#files contains the string that need cleaning
files = [
"Beatles - The Word ",
"The Beatles - The Word",
"Beatles - Tell Me Why",
"Beatles - Tell Me Why (remastered)",
"Beatles - Love me do"
]
#ignore contains the reg expr that need to bee checked
ignore = [/the/,/\(.*\)/,/remastered/,/live/,/remix/,/mix/,/acoustic/,/version/,/ +/]
files.each do |file|
ignore.each do |e|
file.downcase!
file.gsub!(e," ")
file.strip!
end
end
p files
#=>["beatles - word", "beatles - word", "beatles - tell me why", "beatles - tell me why", "beatles - love me do"]
最佳答案
ignore = ["the", "(", ".", "*", ")", "remastered", "live", "remix", "mix", "acoustic", "version", "+"]
re = Regexp.union(ignore)
p re #=> /the|\(|\.|\*|\)|remastered|live|remix|mix|acoustic|version|\+/
Regexp.union
注意逃生。