对于使用Ruby的正则表达式,我有点新奇(或者我认为一般情况下是正则表达式),但我想知道是否有一种实用的方法来匹配使用数组的字符串?Ruby正则表达式匹配数组中的字符串?
让我解释,说我在这种情况下配料表:
1 1/3 cups all-purpose flour
2 teaspoons ground cinnamon
8 ounces shredded mozzarella cheese
最终,我需要将原料分成各自的“量测量”和“成分名”,所以喜欢在的2 teaspoons ground cinnamon
的情况下,将因此而不必像一个巨大的长正则表达式拆分为“8 ounces
,并且shredded mozzarella cheese
:(cup\w*|teaspoon\w*ounce\w* .......)
,我该如何使用一个数组来保存正则表达式之外的值
更新
我这样做(感谢cwninja):
# I think the all units should be just singular, then
# use ruby function to pluralize them.
units = [
'tablespoon',
'teaspoon',
'cup',
'can',
'quart',
'gallon',
'pinch',
'pound',
'pint',
'fluid ounce',
'ounce'
# ... shortened for brevity
]
joined_units = (units.collect{|u| u.pluralize} + units).join('|')
# There are actually many ingredients, so this is actually an iterator
# but for example sake we are going to just show one.
ingredient = "1 (10 ounce) can diced tomatoes and green chilies, undrained"
ingredient.split(/([\d\/\.\s]+(\([^)]+\))?)\s(#{joined_units})?\s?(.*)/i)
这使我接近我想要的东西,所以我觉得这是我想去的方向。
puts "measurement: #{arr[1]}"
puts "unit: #{arr[-2] if arr.size > 3}"
puts "title: #{arr[-1].strip}"
我也使用这种方法,做了一些调整: Regexp.union(测量)代替Regexp.new(measurements.join(“|”)),结果相同,非常干净 – Coelhone 2013-02-20 14:39:28