PHP如何preg匹配此字符串?

时间:2015-01-10 03:23:01

标签: php regex preg-match

我有很长的IP地址和端口列表。我正在尝试从类似于此的列表中匹配端口号:

/connect=;;41.26.162.36;;192.168.0.100;;8081;;
/connect=;;98.250.16.76;;192.168.0.24;;8080;;
/connect=;;216.152.60.12;;192.168.1.103;;8090;;
/connect=;;91.11.65.110;;192.168.1.3;;8081;;

我使用以下方法在匹配全局IP地址方面没有遇到任何问题:

preg_match_all('/connect=;;(.+?);;/', $long, $ip);
            $ip = $ip[1][0];
            print_r($ip);

这对我来说非常合适,但是我无法弄清楚如何预先匹配位于行尾的端口。

3 个答案:

答案 0 :(得分:5)

试试这种方式......

    $re = "/connect=;;(.+?);;(\d{4});;/";
    $str = "/connect=;;41.26.162.36;;192.168.0.100;;8081;;/connect=;;98.250.16.76;;
    192.168.0.24;;8080;;/connect=;;216.152.60.12;;192.168.1.103;;8090;;/connect=;;
    91.11.65.110;;192.168.1.3;;8081;;";

    preg_match_all($re, $str, $matches);

说明:

  /connect=;;(.+?);;(\d{4});;/g

   connect=;; matches the characters connect=;; literally (case sensitive)
  1st Capturing group (.+?)
    .+? matches any character (except newline)
        Quantifier: +? Between one and unlimited times, as few times as possible, 
   expanding as needed [lazy]
   ;; matches the characters ;; literally
  2nd Capturing group (\d{4})
    \d{4} match a digit [0-9]
        Quantifier: {4} Exactly 4 times
  ;; matches the characters ;; literally
  g modifier: global. All matches (don't return on first match)

enter image description here

答案 1 :(得分:5)

稍微扩展一下上面的答案:

if (preg_match_all('/connect=;;([\d\.]+);;([\d\.]+);;(\d+);;/',
                   $long, $matches, PREG_SET_ORDER)) {
  foreach($matches as $match) {
    list(,$ip1, $ip2, $port) = $match;
    /* Do stuff */
    echo "{$ip1} => {$ip2}:{$port}\n";
  }
}

你可以在IP检测上更加聪明,但如果你的样本形成良好,这应该足够了。

http://3v4l.org/FpGFD

答案 2 :(得分:0)

由于您的日志将严格且可靠地格式化,您可能只需扫描格式化的字符串。

https://www.php.net/manual/en/function.sscanf.php

sscanf() 不会像 preg_match_all() 那样产生不需要的全字符串匹配。

代码:(Demo)

$logs = <<<LOGS
/connect=;;41.26.162.36;;192.168.0.100;;8081;;
/connect=;;98.250.16.76;;192.168.0.24;;8080;;
/connect=;;216.152.60.12;;192.168.1.103;;8090;;
/connect=;;91.11.65.110;;192.168.1.3;;8081;;
LOGS;

foreach(explode(PHP_EOL, $logs) as $line) {
    sscanf($line, "/connect=;;%[^;];;%[^;];;%d;;", $ip1, $ip2, $port);
    var_export(
        [$ip1, $ip2, $port]
    );
    echo "\n";
}

输出:

array (
  0 => '41.26.162.36',
  1 => '192.168.0.100',
  2 => 8081,
)
array (
  0 => '98.250.16.76',
  1 => '192.168.0.24',
  2 => 8080,
)
array (
  0 => '216.152.60.12',
  1 => '192.168.1.103',
  2 => 8090,
)
array (
  0 => '91.11.65.110',
  1 => '192.168.1.3',
  2 => 8081,
)