import java.util.regex.Matcher;
import java.util.regex.Pattern; public class DateRegex { public static void main(String[] args) { String DatePattern = "^(?:([0-9]{4}-(?:(?:0?[1,3-9]|1[0-2])-(?:29|30)|" + "((?:0?[13578]|1[02])-31)))|" + "([0-9]{4}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|1\\d|2[0-8]))|" + "(((?:(\\d\\d(?:0[48]|[2468][048]|[13579][26]))|" + "(?:0[48]00|[2468][048]00|[13579][26]00))-0?2-29)))$"; Pattern p = Pattern.compile(DatePattern); Matcher m = p.matcher("2008-2-29"); boolean b = m.matches(); if (b) { System.out.println("matches"); } else System.out.println("no matches"); } } |
|