2010-10-18 135 views
5

对于使用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}" 

回答

22

本人来说我刚刚建立了正则表达式编程,你可以做:

测量值= [...] MEASUREMENTS_RE = Regexp.new(measurements.join(“|”))

...然后使用正则表达式。

只要你保存它,不要重新创建它,它应该是相当有效的。

+7

我也使用这种方法,做了一些调整: Regexp.union(测量)代替Regexp.new(measurements.join(“|”)),结果相同,非常干净 – Coelhone 2013-02-20 14:39:28

3

对于数组一个,这样的事情应该工作:

a.each do |line| 
    parts = /^([\d\s\.\/]+)\s+(\w+)\s+(.*)$/.match(line) 
    # Do something with parts[1 .. 3] 
end 

例如:

a = [ 
    '1 1/3 cups all-purpose flour', 
    '2 teaspoons ground cinnamon', 
    '8 ounces shredded mozzarella cheese', 
    '1.5 liters brandy', 
] 
puts "amount\tunits\tingredient" 
a.each do |line| 
    parts = /^([\d\s\.\/]+)\s+(\w+)\s+(.*)$/.match(line) 
    puts parts[1 .. 3].join("\t") 
end 
+0

+ 1感谢您的回答,奇怪的是您的回答就像我描述我的问题那样愚蠢,我不认为我很清楚,但您的解决方案实际上对我描述的方式非常有益。 – 2010-10-19 07:15:23