i need regex for detect all IPs with ports and without ports but excepting
93.153.31.151(:27002)
and
10.0.0.1(:27002)
I have got some but I need add exception
\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})?
For java matcher
String numIPRegex = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})?";
if (pA.matcher(msgText).find()) {
this.logger.info("Found");
} else {
this.logger.info("Not Found");
}
Without making a statement about better-suited Java classes that can handle IP addreses in a structured manner…
You can add exceptions to a regular expression using negative look-aheads:
Explanation:
(?! # start negative look-ahead (?: # start non-capturing group 93\.153\.31\.151 # exception address #1 | # or 10\.0\.0\.1 # exception address #2 ) # end non-capturing group (?: # start non-capturing group :27002 # port number )? # end non-capturing group; optional ) # end negative look-ahead \d{1,3}(?:\.\d{1,3}){3}(?::\d{1,5})? # your original expressionOf course the other obvious alternative would be to test the exceptions up-front one by one and just return false if one exceptions matches. Wrapping them up all up in one single big regex will quickly become very ugly.