import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?!^xn--.*)(?!^.*(\\.s3alias|--ol-s3)$)(?!^([0-9]+\\.){3}[0-9]+$)(?!.*\\.\\.+)(^[a-z0-9][a-z0-9\\-\\.]{1,61}[a-z0-9]$)$";
final String string = "// ^(?!^xn--.*)(?!^.*(\\.s3alias|--ol-s3)$)(?!^([0-9]+\\.){3}[0-9]+$)(?!.*\\.\\.+)(^[a-z0-9][a-z0-9\\-\\.]{1,61}[a-z0-9]$)$\n"
+ "// 1. Disallow the xn-- .s3alias and --ol-s3 bans:\n"
+ "// (?!^xn--.*)(?!^.*(\\.s3alias|--ol-s3)$)\n"
+ "// 2. Disallow IPv4 address (four segments of 1-3 numbers, separated by dots) \n"
+ "// (?!^([0-9]{1,3}\\.){3}[0-9]{1,3}$)\n"
+ "// 4. Disallow consecutive dots\n"
+ "// (?!.*\\.\\.+)\n"
+ "// 3. must be 3-63 characters, starting and ending with alphanum, and only dots, dashes or lower alphanum\n"
+ "// (^[a-z0-9][a-z0-9\\-\\.]{1,61}[a-z0-9]$)\n"
+ "//\n"
+ "// Shorter version of this: \n"
+ "// ^(?!(xn--.*|.*(\\.s3alias|--ol-s3)$|([0-9]+\\.){3}[0-9]+$|.*\\.\\.+))(^[a-z0-9][a-z0-9\\-\\.]{1,61}[a-z0-9]$)\n"
+ "// \n"
+ "// Alternative:\n"
+ "// ^(?!^xn--.*)(?!^.*(\\.s3alias|--ol-s3)$)(?!^([0-9]+\\.){3}[0-9]+$)^[a-z0-9](\\.[a-z0-9\\-]+|[a-z0-9\\-])*(\\.|[a-z0-9\\-])[a-z0-9]$\n"
+ "// 1. Disallow the xn-- .s3alias and --ol-s3 bans:\n"
+ "// (?!^xn--.*)(?!^.*(\\.s3alias|--ol-s3)$)\n"
+ "// 2. Disallow IPv4 address (four segments of 1-3 numbers, separated by dots) \n"
+ "// (?!^([0-9]{1,3}\\.){3}[0-9]{1,3}$)\n"
+ "// 3. Must start with alphanum\n"
+ "// ^[a-z0-9]\n"
+ "// 4. Allow only dots, dashes or lower alphanum, and disallow consecutive dots:\n"
+ "// (\\.[a-z0-9\\-]+|[a-z0-9\\-])*\n"
+ "// 5. Must be three or more chars and end in an alphanum:\n"
+ "// (\\.|[a-z0-9\\-])[a-z0-9]$\n"
+ "// \n\n\n"
+ "// fine\n"
+ "a.b.c\n"
+ "asdf\n"
+ "a.b\n"
+ "abc\n"
+ "a-b\n"
+ "a--b\n"
+ "a---------------------b\n"
+ "000\n"
+ "010\n"
+ "1.2.3.a\n"
+ "foo-s3alias\n"
+ "foo--ol-s\n"
+ "xn-a\n\n"
+ "// too short\n"
+ "a\n"
+ "ab\n"
+ "// no int'l idn looking\n"
+ "xn--asdf\n"
+ "// can't end in these\n"
+ "foo.s3alias\n"
+ "foo--ol-s3\n"
+ "// must start, end in alphanum\n"
+ "abcd.\n"
+ ".abcd\n"
+ ".ab\n"
+ "abc-\n"
+ "acb.b-\n"
+ "// no consecutive dots\n"
+ "a....c\n"
+ "a..c\n"
+ "aaaa.a.a..b\n"
+ "// non-alphanumdash\n"
+ "élan\n"
+ "hello_world\n"
+ "FOO\n"
+ "foO\n\n";
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