import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:\n"
+ " [dD]{2}[_-]?[mM]{2}[_-]?[yY]{2}(?:[yY]{2})? |\n"
+ " [mM]{2}[_-]?[dD]{2}[_-]?[yY]{2}(?:[yY]{2})? |\n"
+ " [mM]{2}[_-]?[yY]{2}(?:[yY]{2})?[_-]?[dD]{2} |\n"
+ " [dD]{2}[_-]?[yY]{2}(?:[yY]{2})?[_-]?[mM]{2} |\n"
+ " [yY]{2}(?:[yY]{2})?[_-]?[dD]{2}[_-]?[mM]{2} |\n"
+ " [yY]{2}(?:[yY]{2})?[_-]?[mM]{2}[_-]?[dD]{2}\n"
+ ")$";
final String string = "The expected result is that only combinations such as the following can test true:\n"
+ "dd-mm-yy\n"
+ "MM-DD_YYYY\n"
+ "yyyy_dd-MM\n"
+ "mmddyy\n"
+ "YYYYddMM\n"
+ "YY-mm_dd\n"
+ "YYYY-mm_dd\n\n"
+ "And not something like:\n"
+ "ddyyyyymmmmmmmmm\n"
+ "mmddyymm\n\n"
+ "ADDITIONAL no-checks:\n"
+ "mmmmmm\n"
+ "yyyymmmm\n"
+ "yydddd\n"
+ "YYYYddMM-\n"
+ "ddyyyy\n"
+ "yyyymm\n"
+ "yyyydd\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
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