import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^[a-z](\\d\\d?|[a-z]\\d[a-z\\d]?|[a-z]?\\d?\\d \\d[a-z]{2}|[a-z]\\d [a-z] \\d[a-z]{2})$";
final String string = "A0\n"
+ "B11\n"
+ "CC2\n"
+ "DD3D\n"
+ "EE44\n"
+ "F5 5FF\n"
+ "G77 7GG\n"
+ "HH8 8HH\n"
+ "II9 I 9II\n"
+ "JJ00 0JJ\n\n"
+ "00\n"
+ "0A\n"
+ "AA\n"
+ "111\n"
+ "1B1\n"
+ "11B\n"
+ "C1C\n"
+ "1CC\n"
+ "CCC\n"
+ "3333\n"
+ "D333\n"
+ "3D33\n"
+ "33D3\n"
+ "333D\n"
+ "E4E4\n"
+ "E44E\n"
+ "4EE4\n"
+ "4E4E\n"
+ "44EE\n"
+ "DDD3\n"
+ "D3DD\n"
+ "3DDD\n"
+ "DDDD\n";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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