import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^([+-]?\\d*((([02468][048]|[13579][26]|\\d\\d(?!00))([02468][048]|[13579][26]))|\\d{4}(?!-02-29|-366))-((?!02-3|(0[469]|11)-31|000)((0[1-9]|1[012])-(0[1-9]|[12]\\d|30|31)|([012]\\d\\d|3([0-5]\\d|6[0-6])))|(W(?!00)([0-4]\\d|51|52)-[1-7]))|((\\+?\\d*([02468][048]|[13579][26])|-\\d*([02468][159]|[13579][37]))(04|09|15|20|26|32|37|43|48|54|60|65|71|76|82|88|93|99)|(\\+?\\d*([02468][159]|[13579][37])|-\\d*([02468][26]|[13579][048]))(05|11|16|22|28|33|39|44|50|56|61|67|72|78|84|89|95)|(\\+?\\d*([02468][26]|[13579][048])|-\\d*([02468][37]|[13579][159]))(01|07|12|18|24|29|35|40|46|52|57|63|68|74|80|85|91|96)|\\+?\\d*(([02468][37]|[13579][159])(03|14|20|25|31|36|42|53|59|64|70|76|81|87|92|[049]8))|-\\d*(([02468][048]|[13579][26])([059]2|08|13|19|24|30|36|41|47|58|64|69|75|80|86|97)))-W53-[1-7])$";
final String string = "2015-08-10\n"
+ "2015-10-08\n"
+ "12015-08-10\n"
+ "-2015-08-10\n"
+ "+2015-08-10\n"
+ "0015-08-10\n"
+ "1582-10-10\n"
+ "2015-02-28\n"
+ "2016-02-29\n"
+ "2000-02-29\n"
+ "0000-02-29\n"
+ "-2000-02-29\n"
+ "-2016-02-29\n"
+ "200000-02-29\n"
+ "2016-366\n"
+ "2000-366\n"
+ "0000-366\n"
+ "-2016-366\n"
+ "-2000-366\n"
+ "2015-081\n"
+ "2015-W33-1\n"
+ "2015-W53-7\n"
+ " 2015-08-10 \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