简体   繁体   English

正则表达式PHP不起作用

[英]Regular expression php not works

I have this text and i like to detect the adresse ip with preg_match 我有这段文字,我想用preg_match检测到地址ip

Nmap scan report for pc39.home (192.168.1.15)

you find there the regular expression that i have used but it not work the retrun is 0. 您会在此处找到我使用过的正则表达式,但无法正常运行,retrun为0。

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

Thank you in advance. 先感谢您。

Your regular expression is wrong: 您的正则表达式是错误的:

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

You are binding the result to the beginning and the end of the input so it will only match if there is only an ip address between parenthesis and nothing else. 您将结果绑定到输入的开头和结尾,因此只有在括号之间只有一个ip地址且没有其他内容时,它才匹配。

You only need: 您只需要:

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

You can check it regex101 . 您可以检查它regex101

dot is every char. 点是每个字符。 You need to escape it. 您需要逃脱它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM