What regex can I use to get the domain name from a url in Ruby?

后端 未结 4 1049
醉梦人生
醉梦人生 2021-01-12 21:07

I am trying to construct a regex to extract a domain given a url.

for:

http://www.abc.google.com/
http://abc.google.com/
https://www.abc.google.com/
         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-12 21:27

    you may be able to use the domain_name gem for this kind of work. From the README:

    require "domain_name"
    host = DomainName("a.b.example.co.uk")
    host.domain         #=> "example.co.uk"
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题