简体   繁体   中英

How to get domain from email

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

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

If you prefer using a library dedicated to understanding these things:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM