regex - Date format comparmission -


i have question. trying prepare date regex comparmission. problem month , day if 1 digit can present 03 or 3 both month , day. instance possible values:

2015/03/27 or 2015/4/12 or 2015/07/05 or 2015/2/2 or 2015/02/3 

what did far is:

^(?<month>\d(0([0-1]|1[0-2])|([1-12])){1,2})/(?<day>\d{1,2})/(?<year>(?:\d{4}|\d{2}))$ 

i started make month:

(?<month>\d(0([0-1]|1[0-2])|([1-12])){1,2})  (0([0-1]|1[0-2])|([1-12])){1,2}) 

so {1,2} - because can 1 digit or 2 instance (12, 2, 02)

0([0-1]|1[0-2]) | ([1-12])) - because can 2 digits or one

somehow cant figure final version. can me out?

using \d, might end fake dates, 12/67/4567. also, input has date format: year/month/day.

i suggest using regex input format:

^(?<year>(?:19|20)\d{2})\/(?<month>0?[1-9]|1[0-2])\/(?<day>3[01]|0?[1-9]|[12][0-9])$ 

see demo

optional 0s made possible due ? quantifier after 0.

if .net, not have escape /s.

to validate date, use classes , methods of programming environment using. here example in c#:

var resultfromregex = "2015/03/27"; datetime validdate; var isvalid = datetime.tryparseexact(resultfromregex, "yyyy/mm/dd", new system.globalization.cultureinfo("en-us"), system.globalization.datetimestyles.none, out validdate); 

Comments