在PHP中使用正则表达式来匹配名称服?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (35)

这是我的正则表达式: /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

因此,“Name Servers”的奇怪匹配作为一个结果是非复数和大写,而另一个不是。

我正在用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.
提问于
用户回答回答于
用户回答回答于

你可以用

'~(?:\G(?!\A)|Name Servers?:)\h*\K\S+~i'

PHP:

$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
)

扫码关注云+社区

领取腾讯云代金券