我們從這個例子
# 抓出手機號碼
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/
沒有留言:
張貼留言