import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([\\w\\:]+\\:\\w+)|(([\\d\\.]+))";
final String string = "1200:0000:AB00:1234:0000:2552:7777:1313\n"
+ "21DA:D3:0:2F3B:2AA:FF:FE28:9C5A\n"
+ "FE80:0000:0000:0000:0202:B3FF::8329 \n"
+ "0.0.0.0\n"
+ "9.255.255.255\n"
+ "11.0.0.0\n"
+ "126.255.255.255\n"
+ "129.0.0.0\n"
+ "169.253.255.255\n"
+ "169.255.0.0\n"
+ "172.15.255.255\n"
+ "172.32.0.0\n"
+ "191.0.1.255\n"
+ "192.88.98.255\n"
+ "192.88.100.0\n"
+ "192.167.255.255\n"
+ "192.169.0.0\n"
+ "198.17.255.255\n"
+ "223.255.255.255";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html