import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "[0-9]{2}\\/[0-9]{2}\\/[0-9]{4}";
final String string = "Valid dates:\n\n"
+ "01/04/2020\n"
+ "20/01/2019\n"
+ "25/12/1950\n"
+ "30/07/2021\n"
+ "31/08/1919\n"
+ "01/01/1729\n"
+ "10/05/2000\n"
+ "15/10/2049\n\n"
+ "Invalid dates:\n\n"
+ "001/04/1900\n"
+ "01/004/1900\n"
+ "90/04/1900\n"
+ "33/04/1900\n"
+ "01/13/1900\n"
+ "01/04/19000\n"
+ "00/04/1900\n"
+ "01/00/1900\n"
+ "1/4/00\n"
+ "1/04/1900\n"
+ "10/4/1900\n"
+ "10/04/00\n"
+ "///\n"
+ "/04/1900\n"
+ "01//1900\n"
+ "01/04/\n"
+ "01/04/1900\n"
+ "This is the date: 01/04/1900\n"
+ "01.04.1900\n"
+ "01-04-1900\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