这是我的正则表达式:/Name [Ss]ervers?:\s([\S]+\s){0,2}/
由于脚本主要是解析.co.uk和.com域名,我有来自WhoIs的样本结果如下;
.com:Name Server: NS1.EXAMPLE.COM NS2.EXAMPLE.COM
.co.uk Name servers: dns1.registrar-servers.com dns2.registrar-servers.com
因此,“名称服务器”的奇怪匹配是一个结果是非复数和大写,而另一个不是。
我用PHP编写脚本,匹配部分如下:
preg_match('/Name [Ss]ervers?:\s([\S]+\s){0,3}/', $whoisRes, $match)
该代码适用于.com结果,但不适用于.co.uk结果,而且对于我的生活,我不知道为什么!任何帮助将不胜感激!
编辑:以下是针对.co.uk域名的完整WhoIs回复:
WhoIs Result:
Domain name: example-domain.co.uk
Data validation: Nominet was not able to match the registrant's name and/or address against a 3rd party source on 23-Mar-2017
Registrar: eNom LLC [Tag = ENOM]
URL: http://www.enom.com
Relevant dates:
Registered on: 23-Mar-2017
Expiry date: 23-Mar-2019
Last updated: 26-Mar-2018
Registration status: Registered until expiry date.
Name servers: dns1.registrar-servers.com dns2.registrar-servers.com
WHOIS lookup made at 12:26:52 31-May-2018 -- This WHOIS information is provided for free by Nominet UK the central registry for .uk domain names. This information and the .uk WHOIS are: Copyright Nominet UK 1996 - 2018. You may not access the .uk WHOIS or use any data from it except as permitted by the terms of use available in full at https://www.nominet.uk/whoisterms, which includes restrictions on: (A) use of the data for advertising, or its repackaging, recompilation, redistribution or reuse (B) obscuring, removing or hiding any or all of this notice and (C) exceeding query rate or volume limits. The data is provided on an 'as-is' basis and may lag behind the register. Access may be withdrawn or restricted at any time.
显然有比这更多的代码,并且原谅了混乱(我只是在制作实际脚本之前测试数据):
$re = '/(?:\G(?!\A)|Name Servers?:)\h*\K\S+/i';
if (preg_match_all($re, $whoisRes, $matches)) {
$name_servers = $matches;
}
if (preg_match('/Creation Date: ([0-9]+\-[0-9]+\-[0-9]+)/i',
$whoisRes, $match)) {
$domain_date = trim($match[1]);
$creation_date = strtotime(trim($match[1]));
$domainage = dateDiff("now", $creation_date) . "\n";
} elseif (preg_match('/Registered on: ([0-9]+\-[a-zA-Z]+\-[0-9]+)/i',
$whoisRes, $match)) {
$domain_date = trim($match[1]);
$creation_date = strtotime(trim($match[1]));
$domainage = dateDiff("now", $creation_date) . "\n";
} else {
$domainage = 'No Registration Date Found';
}
echo "URL: $url [$online]<br><br>";
echo "Load Time: $response_text [$overall_text]<br><br>";
echo "Name Servers:"; var_dump($name_servers); echo"<br><br>";
echo "Title: $title<br><br>";
echo "Description: $description<br><br>";
echo "Keywords: $keywords<br><br>";
echo "WhoIs Result: $whoisRes<br><br>";
echo "Domain Age: $domainage [$domain_date]<br><br>";
答案 0 :(得分:1)
答案 1 :(得分:1)
您可以使用
'~(?:\G(?!\A)|Name Servers?:)\h*\K\S+~i'
请参阅regex demo
<强>详情
(?:\G(?!\A)|Name Servers?:)
- Name Server:
或Name Servers:
或之前的成功比赛结束\h*
- 0+水平空间\K
- 匹配重置运算符\S+
- 1 +非空白字符。$re = '/(?:\G(?!\A)|Name Servers?:)\h*\K\S+/i';
$str = 'Name Server: NS1.EXAMPLE.COM NS2.EXAMPLE.COM
Name servers: dns1.registrar-servers.com dns2.registrar-servers.com';
if (preg_match_all($re, $str, $matches)) {
print_r($matches[0]);
}
结果:
Array
(
[0] => NS1.EXAMPLE.COM
[1] => NS2.EXAMPLE.COM
[2] => dns1.registrar-servers.com
[3] => dns2.registrar-servers.com
)