import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^([A-Za-z0-9]{2})([0-9]{2})((?:[A-R][0-9]{2}){0,10})((?:[0-9]{4}){0,12})((?:[A-Z])?)$";
final String string = "0101110112011301210122012301310132013301410142014301B\n"
+ "0101B\n"
+ "0101B12B\n"
+ "01011011B\n"
+ "0101B121111B\n"
+ "010110111011B\n"
+ "0101B1210111011B\n"
+ "0520D01D53D\n"
+ "0620E01E53D\n"
+ "0724\n"
+ "0803E23J54B\n"
+ "0903\n"
+ "1006E25J56B\n"
+ "1103B01\n"
+ "1407\n"
+ "1504\n"
+ "1807E30J61B\n"
+ "1904K24R\n"
+ "2003K41M54R\n"
+ "2104K06R\n"
+ "2203H37M68B\n"
+ "72B6P271531\n"
+ "05BRE010941";
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