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

后端 未结 4 1076
醉梦人生
醉梦人生 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:18

    Don't know much about ruby but this regex pattern gives you the last 3 parts of the url excluding the trailing slash with a minumum of 2 characters per part.

    ([\w-]{2,}\.[\w-]{2,}\.[\w-]{2,})/$
    

提交回复
热议问题