Valid dates on regular expression

In Gregorian calendar, if a year is divisible by 400 then it is a leap year. 2000 is a leap year where 2100 is not. how would you implement (if year % 400 == 0) using only regular expression constructs? implementation using if else statement would be considered invalid since this will be dealt with externally. the solution will validate if February has 28 or 29 days. My problem deals with 4 digit year (from 1000) but any guide to a general solution will also be very helpful.

EDIT: Nevermind. Found Guide. http://regexadvice.com/blogs/mash/archive/2004/04/02/Dealing-with-dates-and-leap-years.aspx

Answers


Try This Regex

It will match a leap year.

((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(29)([/])([3579][26]00)$)|(^(0?2)([/])(29)([/])([1][89][0][48])$)|(^(0?2)([/])(29)([/])([2-9][0-9][0][48])$)|(^(0?2)([/])(29)([/])([1][89][2468][048])$)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048])$)|(^(0?2)([/])(29)([/])([1][89][13579][26])$)|(^(0?2)([/])(29)([/])([2-9][0-9][13579][26])$))

It will match

mm/dd/yyyy,m/dd/yyyy,mm,d/yyyy and m/d/yyyy

Check Here


Need Your Help

How to tell if a caught IOException is caused by the file being used by another process, without resorting to parsing the exception's Message property

.net file ioexception robustness

When I open a file, I want to know if it is being used by another process so I can perform special handling; any other IOException I will bubble up. An IOException's Message property contains "The

Implementing message flood protection via JS

javascript protection

I'm currently running a server. A bot that I coded currently supplies administrative tools for moderators, but I would like the bot to also have some sort of automated flood-protection system built...