$re = '/Host: (?:(?:(?:22[0-3]|2[01]\d|1[79][013-9]|16[0-8]|12[0-68-9]|1[013-58]\d|[2-9]\d|1?[1-9])\.(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d))|(?:172\.(?:25[0-5]|2[0-4]\d|1\d{2}|[4-9]\d|3[2-9]|1[0-5]|\d))|(?:192\.(?:25[0-5]|2[0-4]\d|16[0-79]|1[0-57-9][0-9]|[1-9]\d|[0-9]))|(?:169\.(?:25[0-35]|2[0-4]\d|1\d{2}|[1-9]?\d)))(?:\.(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)){2}(?::(?:6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}))?[\r\n]/m';
$str = '-------------------------------------
Testing Criteria (valid and invalid):
-------------------------------------
Host: 212.83.166.45:8080
Host: 87.106.149.74:443
Host: 169.172.234.14:7777
Host: 0.127.134.10:65535
Host: 127.0.0.1:23
Host: 8.8.8.8:65536
Host: 224.0.0.9
Host: 172.128.244.1
Host: 4.22.222.1
Host: 10.11.112.13
Host: 192.155.172.13:1
Host: 172.16.0.1
Host: 172.31.255.255
Host: 192.168.123.213
Host: 173.245.2.23
Host: 192.172.21.5
Host: 192.256.75.1
Host: 8.8.4.4:80
Host: 169.254.23.162
Host: 187.135.2.231
-------------------------------------
Scratch Notes:
-------------------------------------
Combine them:
---------
Host: (?:
(?:(?:22[0-3]|2[01]\\d|1[79][013-9]|16[0-8]|12[0-68-9]|1[013-58]\\d|[2-9]\\d|1?[1-9])\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d))|
(?:172\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[4-9]\\d|3[2-9]|1[0-5]|\\d))|
(?:192\\.(?:25[0-5]|2[0-4]\\d|16[0-79]|1[0-57-9][0-9]|[1-9]\\d|\\d))|
(?:169\\.(?:25[0-35]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d))
)
(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)){2}
---------
Match 0-255:
(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)){2,3}
Exclude Class A, D, E Address Ranges:
0
10
127
224
255
Host: (?:(?:22[0-3]|2[01]\\d|1[013-9]\\d|[1-9]\\d|[1-9])\\.) -- [v1]
<< Note: Forgot, since Class B address ranges will be specified, they need to be excluded in this segment as well >>
169
172
192
Host: (?:(?:22[0-3]|2[01]\\d|1[79][013-9]|16[0-8]|12[0-68-9]|1[013-58]\\d|[2-9]\\d|1?[1-9])\\.) -- [v2]
Exclude Class B Address Ranges:
172.16-31
(?:172\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[4-9]\\d|3[2-9]|1[0-5]|\\d))
192.168
(?:192\\.(?:25[0-5]|2[0-4]\\d|16[0-79]|1[0-57-9][0-9]|[1-9]\\d|\\d))
169.254
(?:169\\.(?:25[0-35]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d))
-------------------------------------
What this PCRE excludes:
-------------------------------------
127.X.X.X
10.X.X.X
169.254.X.X
172.16.X.X
172.31.X.X
192.168.X.X
224.X.X.X (to)
255.X.X.X
-------------------------------------
Now to match valid ports:
-------------------------------------
0-65535
(?::(?:6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3}))?[\\r\\n]';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php