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

前端 未结 7 655
没有蜡笔的小新
没有蜡笔的小新 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:26

    if the URL is in format http://www.google.com, then you could do something like:

    a = 'http://www.google.com'
    puts a.split(/\./)[1] + '.' + a.split(/\./)[2]
    

    Or

    a =~ /http:\/\/www\.(.*?)$/
    puts $1
    

提交回复
热议问题