Page 1 of 1
IP:port regex
Posted: Mon Feb 15, 2010 10:08 am
by klevis miho
I need a regex to get me those:
212.45.5.172:3128
151.100.59.11:3128
58.33.157.74:8080
130.75.87.83:3124
194.42.17.124:3128
Note that the IP's are all valid IP's and I want the port 1-65536.
I would appreciate any help
Re: IP:port regex
Posted: Mon Feb 15, 2010 11:00 am
by AbraCadaver
If using PHP, try parse_url() instead.
Re: IP:port regex
Posted: Mon Feb 15, 2010 11:34 am
by klevis miho
Thank you AbraCadaver but I want basically, in a given text, to extract the ip:port.
Re: IP:port regex
Posted: Mon Feb 15, 2010 4:07 pm
by ridgerunner
Try this one:
Code: Select all
<?php
$contents = "212.45.5.172:3128
151.100.59.11:3128
58.33.157.74:8080
130.75.87.83:3124
194.42.17.124:3128";
$ip_port_re = '/\b # begin on word boundary
( # capture the IP in group 1
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} # first three
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # last IP 0-255
): # end capture group 1
( # capture the port number in group 2
6553[0-5]|
655[0-2]\d|
65[0-4]\d\d|
6[0-4]\d\d\d|
[1-5]\d\d\d\d|
[1-9]\d{0,3}
) # end capture group 2
\b # end on word boundary
/x';
$cnt = preg_match_all($ip_port_re, $contents, $matches, PREG_SET_ORDER);
echo ("$cnt matches found\n");
for ($i = 0; $i < $cnt; $i++) {
echo (sprintf("%3d: IP = %s, PORT = %s\n",
$i + 1, $matches[$i][1], $matches[$i][2]));
}
?>
Hope this helps!
