Man, that's a mess. It's better to simplify the search pattern, and then validate values, rather than try to do everything in the regex. e.g.,
private String findIP(String in) {
Matcher m = Pattern.compile("((\\d+\\.){3}\\d+):(\\d+)").matcher(in);
if (m.find()) {
String[] p = m.group(1).split("\\.");
for (int i = 0; i < 4; i++)
if (Integer.parseInt(p[i]) > 255) return null;
if (Integer.parseInt(m.group(3)) > 65535) return null;
return m.group(0);
}
return null;
}
I'm probably not taking everything into account (addresses starting with 0., parseInt returning negatives, etc.), but having a simpler regex pays off for readability, as well as having the numbers 4, 255, and 65535 be cues to the future reader that we're talking about a dotted quad and a port.