import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:(?=(\\d)..\\1(?!0*$)|(?:0..1|1..2|2..3|3..4|4..5|5..6|6..7|7..8|8..9|9..0)0*$)\\d){3}\\d{3}$";
final String string = "Pass:\n"
+ "000001\n"
+ "009010\n"
+ "010011\n"
+ "019020\n"
+ "057058\n"
+ "099100\n"
+ "100101\n"
+ "299300\n"
+ "398399\n"
+ "404405\n"
+ "500501\n"
+ "665666\n"
+ "666667\n"
+ "899900\n"
+ "998999\n\n"
+ "Edge Case:\n"
+ "999000\n\n"
+ "Not Pass:\n"
+ "000000\n"
+ "123456\n"
+ "123234\n"
+ "000002\n"
+ "010002\n"
+ "010012\n"
+ "050060\n"
+ "397399\n"
+ "0071072\n"
+ "89090\n"
+ "0890900\n"
+ "1234\n"
+ "200210\n"
+ "123123124";
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