import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^\\s*[+-]?(\\d{4,10}-((00[1-9]|0[1-9]\\d|[12]\\d\\d|3[0-5]\\d|36[0-5])|(0[1-9]|1[0-2])-(0[1-9]|1\\d|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31|W(0[1-9]|[1-4]\\d|5[0-2])-[1-7]))|((\\d{2,8}([13579][26]|[2468][048]|0[48])|(\\d{0,6}([13579][26]|[02468][048])00))-(366|02-29))|(\\+?\\d{0,6}(([02468][048]|[13579][26])([26]0|71|[38]2|[49]3|[05]4|15|[27]6|37|[48]8|[09]9)|([02468][159]|[13579][37])(50|[16]1|[27]2|33|[48]4|[09]5|[15]6|67|[27]8|[38]9)|([02468][26]|[13579][048])([48]0|[09]1|[15]2|63|[27]4|[38]5|[49]6|[05]7|[16]8|29)|([02468][37]|[13579][159])([27]0|[38]1|[49]2|[05]3|[16]4|25|[37]6|87|[049]8|[5]9))|-\\d{0,6}(([02468][048]|[13579][26])(0[28]|1[39]|24|3[06]|4[17]|5[28]|6[49]|75|8[06]|9[27])|([02468][159]|[13579][37])(0[49]|15|2[06]|3[27]|4[38]|54|6[05]|7[16]|8[28]|9[39])|([02468][26]|[13579][048])(0[51]|16|2[28]|3[39]|44|5[06]|6[17]|7[28]|8[49]|95)|([02468][37]|[13579][159])(0[17]|1[28]|2[49]|35|4[06]|5[27]|6[38]|74|8[05]|9[16])))-W53-[1-7]\\s*$";
final String string = "2015-00-10\n"
+ "2015-13-10\n"
+ "2015-08-00\n"
+ "2015-08-32\n"
+ "2015-04-31\n"
+ "2015-02-30\n"
+ "2015-02-29\n"
+ "2100-02-29\n"
+ "-2100-02-29\n"
+ "2015-000\n"
+ "2015-366\n"
+ "2016-367\n"
+ "2100-366\n"
+ "-2100-366\n"
+ "2015-W00-1\n"
+ "2015-W54-1\n"
+ "2016-W53-1\n"
+ "2015-W33-0\n"
+ "2015-W33-8\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