How can I parse urls like
http://www.1800contacts.com/productlist.aspx?dl=P&source=cj&ac=8.2.0007
and only get
http://www.1800contacts.com
?
PS. Some urls have subdomains etc so I can't use regexps here.
Try to use 'uri' library:
require 'uri'
address = 'http://www.1800contacts.com/productlist.aspx?dl=P&source=cj&ac=8.2.0007'
uri = URI.parse(address)
puts "#{uri.scheme}://#{uri.host}" # => http://www.1800contacts.com
2 alternative ways
uri = URI.parse(url); uri.path = ''; uri.query = nil; uri.to_s
url.split('/')[0,3].join('/')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With