import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(January|February|March|April|May|June|July|August|September|October|November|December) (3[01]|[12][0-9]|0[1-9]) (19|20)\\d{2}$";
final String string = "Should pass:\n\n"
+ "January 12 1975\n"
+ "February 31 2000\n"
+ "May 01 2099\n\n"
+ "Should fail:\n\n"
+ "Abbreviated month:\n"
+ "Jan 12 1975\n\n"
+ "Day out of range:\n"
+ "February 32 2000\n\n"
+ "No leading zero on day:\n"
+ "May 1 2099\n\n"
+ "Year out of range:\n"
+ "March 11 1234\n\n"
+ "Year too long:\n"
+ "May 01 20000\n\n"
+ "Year too short:\n"
+ "May 01 19\n\n";
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