0

I have this text and i like to detect the adresse ip with preg_match

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.

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

Thank you in advance.

Ktari zied
  • 157
  • 1
  • 2
  • 13
  • 3
    You asked the same question here: http://stackoverflow.com/questions/37294578/regular-expression-to-match-ip-addresses – Andreas May 24 '16 at 09:23

2 Answers2

0

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.

You only need:

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

You can check it regex101.

jeroen
  • 91,079
  • 21
  • 114
  • 132
0

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);
nospor
  • 4,190
  • 1
  • 16
  • 25