How would you parse a url in Ruby to get the main domain?

前端 未结 7 638
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 02:03

I want to be able to parse any url with ruby to get the main part of the domain without the www (just the XXXX.com)

7条回答
  •  独厮守ぢ
    2020-11-30 02:18

    Here's one that works better with .co.uk and .com.fr - type domains

    domain = uri.host[/[^.\s\/]+\.([a-z]{3,}|([a-z]{2}|com)\.[a-z]{2})$/]
    

提交回复
热议问题