Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP How to preg match this string?

I have long list of IP addresses and ports. I'm trying to preg match the port number from a list that looks similar to this:

/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;;

I have had no problems preg matching the global IP address using:

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

This works perfectly for me, but i cannot figure out how to preg match the port which sits at the end of the line.

like image 651
user1294097 Avatar asked Dec 26 '22 00:12

user1294097


2 Answers

Just to expand on the above answer a bit:

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";
  }
}

You could get a bit more clever on the IP detection, but if your samples are well formed, this should be more than enough.

http://3v4l.org/FpGFD

like image 104
Sara Avatar answered Dec 28 '22 08:12

Sara


Try this way ...

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

Explanation:

  /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

like image 36
Always Sunny Avatar answered Dec 28 '22 08:12

Always Sunny