Created
February 21, 2013 12:53
-
-
Save atton/5004550 to your computer and use it in GitHub Desktop.
2013/02/20 な Okinawa.rb の 正規表現でキャプチャしたのを変数に入れたりマッチオブジェクトにしたりなアレ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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