的范围之前我有2个字符串:Ruby的正则表达式匹配的字符串与数字
I have 4 cars in my house
I have 14 cars in my house
我们如何使用红宝石(1.9.3)的正则表达式来检查只有1到10辆匹配?
例:
I have 1 car in my house # => match
I have 4 cars in my house # => match
I have 10 cars in my house # => match
I have 14 cars in my house # => should not match
I have 100 cars in my house # => should not match
此外,我们该如何匹配(即2辆)针对任何字符串?所以如果目标字符串包含'22辆汽车',那么它不应该匹配。
例:
some other string before 2 cars some other string after # => match
some other string before 22 cars some other string after # => should not match
'/我有(1辆车| [2-9]车| 10辆车)在我家/'? – Howard 2012-03-24 16:46:56
你的车不应该在你的车库? :D霍华德提供了正确的答案。我第一次错过了单数形式。 – alfa 2012-03-24 16:51:00