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
IP:port regex
Moderator: General Moderators
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: IP:port regex
If using PHP, try parse_url() instead.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
Re: IP:port regex
Thank you AbraCadaver but I want basically, in a given text, to extract the ip:port.
- ridgerunner
- Forum Contributor
- Posts: 214
- Joined: Sun Jul 05, 2009 10:39 pm
- Location: SLC, UT
Re: IP:port regex
Try this one:
Hope this helps! 
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]));
}
?>