Regex 如何从URL获取域名
如何从URL字符串中获取域名 示例: 相关的:Regex 如何从URL获取域名,regex,url,Regex,Url,如何从URL字符串中获取域名 示例: 相关的: 我不知道有任何库,但是域名的字符串操作非常简单 困难的部分是知道这个名字是第二级还是第三级。为此,您需要维护一个数据文件(例如For.uk并不总是第三级,一些组织(例如bl.uk、jet.uk)存在于第二级) 来自Mozilla的有这样一个数据文件,请检查Mozilla许可证,看看是否可以重用该文件。您需要一个可以删除哪些域前缀和后缀的列表。例如: 前缀: www. 后缀: .com .co.in .au.uk 我曾经为一家公司写过这样的
www.
.com
.co.in
.au.uk
- 获取每一个可用项目的列表。你的第一站应该是。Mozilla的列表乍一看非常棒,但缺少ac.uk,因此它实际上不可用
- 像下面的例子一样加入列表警告:订购很重要!如果org.uk出现在uk之后,那么example.org.uk将匹配org,而不是example李>
- 我不知道有任何库,但是域名的字符串操作非常简单
困难的部分是知道这个名字是第二级还是第三级。为此,您需要维护一个数据文件(例如For.uk并不总是第三级,一些组织(例如bl.uk、jet.uk)存在于第二级)
来自Mozilla的有这样一个数据文件,请检查Mozilla许可证,看看是否可以重用该文件。您需要一个可以删除哪些域前缀和后缀的列表。例如: 前缀:
.*([^\.]+)(com|net|org|info|coop|int|co\.uk|org\.uk|ac\.uk|uk|__and so on__)$
这真的很好,也符合像de.com和friends这样奇怪的非官方高层
好处是:
- 如果正则表达式是最优排序的,则速度非常快
- 手写正则表达式,如果CCTLD更改或添加,则必须手动更新。乏味的工作李>
- 非常大的正则表达式,因此不太可读
导入URL解析
通用\u TLD=[
‘航空’、‘亚洲’、‘商务’、‘com’、‘coop’、‘edu’、‘政府’、‘信息’、‘国际’、‘工作’,
“mil”、“mobi”、“museum”、“name”、“net”、“org”、“pro”、“tel”、“travel”、“cat”
]
def get_域(url):
hostname=urlparse.urlparse(url.lower()).netloc
如果主机名=“”:
#强制识别为完整URL
hostname=urlparse.urlparse('http://'+uri).netloc
#删除“user:passw”、“www.”和“:port”部分
hostname=hostname.split('@')[-1]。split(':')[0]。lstrip('www.')。split('.'))
num\u parts=len(主机名)
如果(num_parts<3)或(len(主机名[-1])>2):
返回“.”。加入(主机名[:-1])
如果len(主机名[-2])>2且主机名[-2]不在通用\u TLD中:
返回“.”。加入(主机名[:-1])
如果num_parts>=3:
返回“.”。加入(主机名[:-2])
这段代码不能保证适用于所有URL,也不能过滤语法正确但无效的URL,比如“example.uk”
不过,在大多数情况下,它都能完成任务。/[^w{3}.]([a-zA-Z0-9]([a-zA-Z0-9-]{0,65}[a-zA-Z0-9])?\)+[a-zA-Z]{2,6}/gim
使用此javascript正则表达式会忽略www和下面的点,同时保持域的完整性。也没有正确匹配www和cc tld如果不使用tld列表,就不可能与它们进行比较,因为它们存在许多类似或的情况,这些情况将被正则表达式解释为域
db.de
(正确)和co.uk
(错误)
但即使这样,如果你的列表中也没有SLD,你也不会成功。URL类似并将被解释为uk.com
(第一个域是big.uk.com)
因此,所有浏览器都使用Mozilla的公共后缀列表:
通过以下URL导入,您可以在代码中使用它:
请随意扩展我的功能提取域名,只。它不会使用正则表达式,而且速度很快:
因此,如果您只有一个字符串而没有一个window.location,那么您可以使用
String.prototype.toUrl = function(){
if(!this && 0 < this.length)
{
return undefined;
}
var original = this.toString();
var s = original;
if(!original.toLowerCase().startsWith('http'))
{
s = 'http://' + original;
}
s = this.split('/');
var protocol = s[0];
var host = s[2];
var relativePath = '';
if(s.length > 3){
for(var i=3;i< s.length;i++)
{
relativePath += '/' + s[i];
}
}
s = host.split('.');
var domain = s[s.length-2] + '.' + s[s.length-1];
return {
original: original,
protocol: protocol,
domain: domain,
host: host,
relativePath: relativePath,
getParameter: function(param)
{
return this.getParameters()[param];
},
getParameters: function(){
var vars = [], hash;
var hashes = this.original.slice(this.original.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
};};
出于某种目的,我昨天完成了这个快速Python函数。它从URL返回域。它的速度很快,不需要任何输入文件列出的东西。然而,我并不是假装它在所有情况下都能工作,但它确实完成了一个简单的文本挖掘脚本所需要的工作 输出如下所示: =>google.co.uk
=>tumblr.com
def getDomain(url):
parts = re.split("\/", url)
match = re.match("([\w\-]+\.)*([\w\-]+\.\w{2,6}$)", parts[2])
if match != None:
if re.search("\.uk", parts[2]):
match = re.match("([\w\-]+\.)*([\w\-]+\.[\w\-]+\.\w{2,6}$)", parts[2])
return match.group(2)
else: return ''
看起来效果不错。但是,必须对其进行修改,以便根据需要删除输出上的域扩展。基本上,您需要的是:
google.com -> google.com -> google
www.google.com -> google.com -> google
google.co.uk -> google.co.uk -> google
www.google.co.uk -> google.co.uk -> google
www.google.org -> google.org -> google
www.google.org.uk -> google.org.uk -> google
可选:
www.google.com -> google.com -> www.google
images.google.com -> google.com -> images.google
mail.yahoo.co.uk -> yahoo.co.uk -> mail.yahoo
mail.yahoo.com -> yahoo.com -> mail.yahoo
www.mail.yahoo.com -> yahoo.com -> mail.yahoo
您不需要构造一个不断变化的正则表达式,因为如果您只需查看名称的最后第二部分,99%的域将正确匹配:
(co|com|gov|net|org)
如果是其中一个,则需要匹配3个点,否则为2个点。简单。现在,我的正则表达式魔法无法与其他一些SO'ER的相比,因此我发现实现这一点的最佳方法是使用一些代码,假设您已经删除了路径:
my @d=split /\./,$domain; # split the domain part into an array
$c=@d; # count how many parts
$dest=$d[$c-2].'.'.$d[$c-1]; # use the last 2 parts
if ($d[$c-2]=~m/(co|com|gov|net|org)/) { # is the second-last part one of these?
$dest=$d[$c-3].'.'.$dest; # if so, add a third part
};
print $dest; # show it
根据您的问题,仅获取名称:
my @d=split /\./,$domain; # split the domain part into an array
$c=@d; # count how many parts
if ($d[$c-2]=~m/(co|com|gov|net|org)/) { # is the second-last part one of these?
$dest=$d[$c-3]; # if so, give the third last
$dest=$d[$c-4].'.'.$dest if ($c>3); # optional bit
} else {
$dest=$d[$c-2]; # else the second last
$dest=$d[$c-3].'.'.$dest if ($c>2); # optional bit
};
print $dest; # show it
我喜欢这种方法,因为它是免维护的。除非你想确认它实际上是一个合法的域,但这是毫无意义的,因为你很可能只是用它来处理日志文件,而一个无效的域在一开始就找不到它
如果您想匹配“非官方”子域,如bozo.za.net或bozo.au.uk,bozo.msf.ru只需在正则表达式中添加(za | au | msf)
我很想看到有人只用一个正则表达式来完成所有这些,我相信这是可能的。使用这个
/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/i
(.)(.*?)(.)
然后,只提取前导点和终点。
简单,对吗?有两种方法
使用拆分
然后解析这个字符串
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf('://') > -1) {
domain = url.split('/')[2];
} if (url.indexOf('//') === 0) {
domain = url.split('/')[2];
} else {
domain = url.split('/')[0];
}
//find & remove port number
domain = domain.split(':')[0];
使用Regex
var r = /:\/\/(.[^/]+)/;
"http://stackoverflow.com/questions/5343288/get-url".match(r)[1]
=> stackoverflow.com
希望这对知识有所帮助:
'http://api.livreto.co/books'.replace(/^(https?:\/\/)([a-z]{3}[0-9]?\.)?(\w+)(\.[a-zA-Z]{2,3})(\.[a-zA-Z]{2,3})?.*$/, '$3$4$5');
# returns livreto.co
=((?:(?:(?:)http)s:)?\/\/)(?:(?:[a-zA-Z0-9]+)\。*(?:(?:[a-zA-Z0-9]+)\.[a-zA-Z0-9]{2,3}
(您可能希望在模式的末尾添加“\/” my @d=split /\./,$domain; # split the domain part into an array
$c=@d; # count how many parts
if ($d[$c-2]=~m/(co|com|gov|net|org)/) { # is the second-last part one of these?
$dest=$d[$c-3]; # if so, give the third last
$dest=$d[$c-4].'.'.$dest if ($c>3); # optional bit
} else {
$dest=$d[$c-2]; # else the second last
$dest=$d[$c-3].'.'.$dest if ($c>2); # optional bit
};
print $dest; # show it
/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/i
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf('://') > -1) {
domain = url.split('/')[2];
} if (url.indexOf('//') === 0) {
domain = url.split('/')[2];
} else {
domain = url.split('/')[0];
}
//find & remove port number
domain = domain.split(':')[0];
var r = /:\/\/(.[^/]+)/;
"http://stackoverflow.com/questions/5343288/get-url".match(r)[1]
=> stackoverflow.com
'http://api.livreto.co/books'.replace(/^(https?:\/\/)([a-z]{3}[0-9]?\.)?(\w+)(\.[a-zA-Z]{2,3})(\.[a-zA-Z]{2,3})?.*$/, '$3$4$5');
# returns livreto.co
function parse_url_all($url){
$url = substr($url,0,4)=='http'? $url: 'http://'.$url;
$d = parse_url($url);
$tmp = explode('.',$d['host']);
$n = count($tmp);
if ($n>=2){
if ($n==4 || ($n==3 && strlen($tmp[($n-2)])<=3)){
$d['domain'] = $tmp[($n-3)].".".$tmp[($n-2)].".".$tmp[($n-1)];
$d['domainX'] = $tmp[($n-3)];
} else {
$d['domain'] = $tmp[($n-2)].".".$tmp[($n-1)];
$d['domainX'] = $tmp[($n-2)];
}
}
return $d;
}
$urls = array('www.test.com', 'test.com', 'cp.test.com' .....);
echo "<div style='overflow-x:auto;'>";
echo "<table>";
echo "<tr><th>URL</th><th>Host</th><th>Domain</th><th>Domain X</th></tr>";
foreach ($urls as $url) {
$info = parse_url_all($url);
echo "<tr><td>".$url."</td><td>".$info['host'].
"</td><td>".$info['domain']."</td><td>".$info['domainX']."</td></tr>";
}
echo "</table></div>";
def urlextract(url):
url_split=url.split(".")
if len(url_split) <= 2:
raise Exception("Full url required with subdomain:",url)
return {'subdomain': url_split[0], 'domain': url_split[1], 'suffix': ".".join(url_split[2:])}
window.location.host.match(/(\w|-)+(?=(\.(com|net|org|info|coop|int|co|ac|ie|co|ai|eu|ca|icu|top|xyz|tk|cn|ga|cf|nl|us|eu|de|hk|am|tv|bingo|blackfriday|gov|edu|mil|arpa|au|ru)(\.|\/|$)))/g)[0]