2016-05-24 58 views
0

我有这样的文字,我喜欢来检测住址IP与preg_match正则表达式PHP不工作

Nmap scan report for pc39.home (192.168.1.15) 

你会发现那里,我已经使用正则表达式,但它不能正常工作的retrun为0

$regex=preg_match('/^\(([\d.]+)\)$/', $scan, $out); 

预先感谢您。

+3

你在这里问同样的问题:http://stackoverflow.com/questions/37294578/regular-expression-to-match-ip-addresses – Andreas

回答

0

你的正则表达式是错误的:

^\(([\d.]+)\)$ 
^ here ^

您具有约束力的结果开始和输入结束,所以如果有只有括号,没有别的之间的IP地址,将只匹配。

你只需要:

\(([\d.]+)\) 

您可以检查它regex101

+0

谢谢$重新=“/ \\(([\ \ d] +)\\)/“。 \t $ regex = preg_match($ re,$ scan,$ out); –

0

点是每个字符。你需要逃避它。

$scan = 'Nmap scan report for pc39.home (192.168.1.15)'; 
$regex=preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $scan, $out); 
print_r($out); 
+0

不是在一个字符类''[']' – jeroen

+0

是的,你是对的:) – nospor