的範圍之前我有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