這個網站可以針對你的需要處理的字串,透過你所寫的RegExp馬上顯示出來所截取的資料
2013年1月2日 星期三
Ruby Regular Expressions
我們從這個例子
# 抓出手機號碼
phone = "123-456-7890"
if phone =~ /(\d{3})-(\d{3})-(\d{4})/
ext = $1
city = $2
num = $3
end
可以抓出分別是123, 456, 7890
但在新的一版1.9可以在正規表示內加入參數
但在新的一版1.9可以在正規表示內加入參數
# 抓出手機號碼
match_phone = /(?<ext>\d{3})-(?<city>\d{3})-(?<num>\d{4})/.match(phone)
puts match_phone[:ext]
puts match_phone[:city]
puts match_phone[:num]
參考資料
ihower
http://www.igvita.com/2011/02/03/new-ruby-19-features-tips-tricks/
訂閱:
意見 (Atom)
