2013-02-11 34 views
1

我用的是國家的寶石從hexorx,這裏是鏈接到寶石
Countries gem寶石國家使用正則表達式

我在代碼country.rb看到,給正則表達式作爲搜索全國參數是可能的。問題是,我甚至不知道如何使用Regexp。
我想要做的是,例如,給我所有以「T」開頭的國家。我試着用這個

1.9.3-p327 :013 > c = Country.find_all_countries_by_name("/(T*)/") 
=> [] 

你怎麼看,根本沒什麼功夫。

回答

0

是搭配T開始所有國家會看起來像:

c = Country.find_all_countries_by_name("/^T[A-Za-z ]*/") 

在這種情況下,你正在做以下幾點:

/ - start of the match 
^ - matches the start of the string (so the next character MUST be first) 
T - literal T 
[A-Za-z ] - a "character class" allowing any a-z upper or lower plus space 
* - repeat previous character (or character class) 0-many times 

這是學習正則表達式的重要資源:http://www.regular-expressions.info/