import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\(20(\\d{2}|x{2})((\\.|-)([0-9]{2}|x{2}|KW(\\d{2}|x{2})|Q(x|[1-4])|(([0-9]{2}|x{2})(\\.|-)([0-9]{2}|x{2}))))?\\)";
final String string = "20000002000\n"
+ "200 0\n"
+ "(2022.xx2000.xx)\n"
+ "(2022.xx) 2000.xx\n"
+ "(2020-20)\n"
+ "(20xx-12)\n"
+ "(20xx.xx-11)\n"
+ "(2002-KW00.00)\n"
+ "(2002-KW00)\n"
+ "(2022.KWxx)\n"
+ "(2021.KW09)\n"
+ "(2021.KW09)\n"
+ "(2021.02)\n"
+ "2022.Qx\n"
+ "2032.Q4\n"
+ "(2020.xx)\n"
+ "2022-Q1\n"
+ "2022 Test\n"
+ "Test 20xx\n\n"
+ "20202020\n"
+ ".Qx\n"
+ "KW23\n"
+ "KWxx\n"
+ "(20xx)\n"
+ "(.-Q4)\n\n"
+ "2000-\n"
+ "2000.\n\n";
final Pattern pattern = Pattern.compile(regex);
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