如何通过电子邮件获取域名

时间:2011-01-15 13:13:34

标签: ruby-on-rails ruby

如何从ruby中的电子邮件地址获取域名?

3 个答案:

答案 0 :(得分:62)

>> "hey@mycorp.com".split("@").last
=> "mycorp.com"

答案 1 :(得分:43)

如果您更喜欢使用专门用于理解这些内容的图书馆:

→ irb -rmail
ruby-1.9.2-p0 > Mail::Address.new('feep+wibble@example.com').domain
 => "example.com" 

答案 2 :(得分:2)

    >>  email = "Sahil Grover<sahil+test@stackoverflow.com>"        
    => "Sahil Grover<sahil+test@stackoverflow.com>"

    >> mail = Mail::Address.new(email)         
    => #<Mail::Address:75152940 Address: |Sahil Grover <sahil+test@stackoverflow.com>| >

    >> mail.instance_values        
    => {"output_type"=>:decode,
       "parsed"=>true,
     "data"=>#<struct Mail::Parsers::AddressStruct 
     raw="Sahil Grover<sahil+test@stackoverflow.com>", 
     domain="stackoverflow.com", 
     comments=[], 
     local="sahil+test", 
     obs_domain_list=nil, 
     display_name="Sahil Grover", 
     group=nil, 
     error=nil>,
     "display_name"=>"Sahil Grover"}

    >> mail.domain        
    => "stackoverflow.com"