Skip to content

Instantly share code, notes, and snippets.

@atton
Created February 21, 2013 12:53
Show Gist options
  • Save atton/5004550 to your computer and use it in GitHub Desktop.
Save atton/5004550 to your computer and use it in GitHub Desktop.
2013/02/20 な Okinawa.rb の 正規表現でキャプチャしたのを変数に入れたりマッチオブジェクトにしたりなアレ
# vim:set fileencoding=utf-8:
string = "http://www.example.com/hoge/fuga"
/(?<protocol>[a-z]+):\/\/(?<server>[^\/]*)\/(?<path>.*)/ =~ string
p [protocol, server, path] # => ["http", "www.example.com", "hoge/fuga"]
m = /(?<protocol>[a-z]+):\/\/(?<server>[^\/]*)\/(?<path>.*)/.match(string)
p m
#=> #<MatchData "http://www.example.com/hoge/fuga" protocol:"http" server:"www.example.com" path:"hoge/fuga">
p m[:protocol] #=> "http"
p m["protocol"] #=> "http"
p [m[:protocol], m[:server], m[:path]] #=> ["http", "www.example.com", "hoge/fuga"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment