import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:(?:[0-2]\\d|[3][0-1]|\\d)\\.(?:[0][1-9]|[1][0-2]|\\d)\\.\\d*|\\d*-(?:[0][1-9]|[1][0-2]|\\d)-(?:[0-2]\\d|[3][0-1]|\\d))$";
final String string = "18.03.2018\n"
+ "20.10.1967\n"
+ "03.05.2002\n"
+ "3.5.2002\n"
+ "03.5.2002\n"
+ "3.05.2002\n"
+ "18.03.18\n"
+ "20.10.67\n"
+ "03.05.02\n"
+ "3.5.02\n"
+ "03.5.02\n"
+ "3.05.02\n\n"
+ "2018-05-3\n"
+ "2018-06-03\n"
+ "1967-6-2\n\n\n\n"
+ "32.13.2001\n"
+ "15.15.2002\n"
+ "-1.03.3002\n"
+ "20.10.6a7\n"
+ "a20.10.67\n"
+ "20.10a.67";
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