import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?i)(?:[\\da-f]{0,4}:){1,7}(?:(?<ipv4>(?:(?:25[0-5]|2[0-4]\\d|1?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4]\\d|1?\\d\\d?))|[\\da-f]{0,4})";
final String string = "2001:db8:3333:4444:5555:6666:7777:8888\n"
+ "2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF\n"
+ "2001:0db8:0001:0000:0000:0ab9:C0A8:\n"
+ "fffe:3465:efab:23fe:2235:6565\n"
+ "2001:0000:6dcd:8c74:::ac32:6a1\n"
+ "2345:0425:2CA1:::5673:23b5\n"
+ "2001:db8:1::ab9:C0A8:102\n"
+ "2001:db8::1234:5678\n"
+ "FF02:0:0:0:0:0:0:2\n"
+ "fdf8:f53b:82e4::53\n"
+ "fe80::200:5aee:feaa:20a2\n"
+ "2001:0000:4136:e378:\n"
+ "8000:63bf:3fff:fdd2\n"
+ "2001:db8::\n"
+ "::1234:5678\n"
+ "2000::\n"
+ "2001:db8:a0b:12f0::1\n"
+ "2001:4:112:cd:65a:753:0:a1\n"
+ "2001:0002:6c::430\n"
+ "2001:5::\n"
+ "fe08::7:8\n"
+ "2001:10:240:ab::a\n"
+ "2002:cb0a:3cdd:1::1\n"
+ "2001:db8:8:4::2\n"
+ "ff01:0:0:0:0:0:0:2\n\n\n"
+ "1:2:3:4:5:6:7:8\n"
+ "1::3:4:5:6:7:8\n"
+ "1::4:5:6:7:8\n"
+ "1:2:3::5:6:7:8\n"
+ "1::7:8\n"
+ "1:2:3:4:5::7:8\n"
+ "1:2:3:4:5::8\n"
+ "::2:3:4:5:6:7:8\n"
+ "::8\n"
+ "::\n"
+ "::1\n\n"
+ "::ffff:192.0.2.47\n"
+ "::255.255.255.255\n"
+ "::ffff:255.255.255.255 \n"
+ "::ffff:0:255.255.255.255\n"
+ "2001:db8:3:4::192.0.2.33\n"
+ "64:ff9b::192.0.2.33\n"
+ "2001:db8:3333:4444:5555:6666:1.2.3.4\n"
+ "24a6:57:c:36cf:0000:5efe:109.205.140.116\n"
+ "::11.22.33.44\n"
+ "::10.0.0.3\n"
+ "2001:db8::123.123.123.123\n"
+ "::ffff:10.0.0.3\n"
+ "::1234:5678:91.123.4.56\n"
+ "::1234:5678:1.2.3.4\n"
+ "2001:db8::1234:5678:5.6.7.8\n"
+ "2001:db8:3:4:5::192.0.2.33";
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