mightymouse3062
mightymouse3062

Reputation: 109

Multiple Delimiter Date Regular Expression

I am working on creating a regular expression to verify a date in multiple formats and I have found plenty of info on how to handle a date in the following formats:

06/19/2013
06-19-2013
06 19 2013
06.19.2013

but I cannot seem to get the regex to handle

06192013

Here is my regular expression:

((0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.]((19|20)?[0-9]{2}

How do I handle the date with no delimiters?

Upvotes: 3

Views: 1216

Answers (2)

Bergi
Bergi

Reputation: 664385

Just make it optional:

(0?[1-9]|1[012])[- \/.]?(0?[1-9]|[12][0-9]|3[01])[- \/.]?(19|20)?[0-9]{2}
                       ^                                ^

Upvotes: 3

Jerry
Jerry

Reputation: 4408

Why don't you use DateTime.TryParseExact()?

http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx

Upvotes: 0

Related Questions