Pattern pattern = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(\\d|[1-2]\\d|3[0-2]))?$");
Matcher matcher = pattern.matcher("84.240.40.0/24");
if (matcher.find()) {
System.out.println(matcher.group());
}
Output:
84.240.40.0/24
this pattern matches IPv4 address and IPv4 CIDR range if you want tomatch only range ,you should remove last question mark