1

我需要检查 aURL是否与domain Ex 匹配:

http://www.opera.com/docs/changelogs/mac/ 匹配:opera.com

http://www.amazon.co.uk/gp/feature.html%3Fie%3DUTF8%26docId%3D1000423923 47 匹配 amazon.co.uk

为此,我写了这个:

require 'net/http'

uri = URI('http://cran.r-url.org/bin/max/')
domain = 'r-url.org'

urlToMatch = uri.host
check = urlToMatch.match(domain)


if check
  print "match \n"
else
  print "not a match \n"
end

有没有更好的方法来做到这一点?谢谢

4

2 回答 2

3
require 'uri'

def url_on_domain?(url, domain)
  URI.parse(url).host.match(domain)
end

if url_on_domain?('http://cran.r-url.org/bin/max/', 'r-url.org')
  print "match \n"
else
  print "not a match \n"
end
于 2013-10-07T10:09:26.133 回答
0
require 'uri'

domain = 'r-url.org'
check = URI.parse('http://cran.r-url.org/bin/max/').host == domain

if check
  print "match \n"
else
  print "not a match \n"
end
于 2013-10-07T09:22:55.207 回答