0

I want to use regular expression for matching these date formats as below in C#.

  1. YYYY/MM/DD 2013/11/12
  2. YYYY/M/DD 2013/5/11
  3. YYYY/MM/D 2013/10/5
  4. YYYY/M/D 2013/5/6

I have tried some regular expressions but they can't match the 4 date formats. such as

^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Michael
  • 163
  • 1
  • 4
  • 12

6 Answers6

4

check this to get an idea of the compexity of regex and validating dates. so i would use

\d{4}(?:/\d{1,2}){2}

then in c# do whatever to validate the match. while it can be done, you'll be spending a lot of time trying to achieve it, though there is a regex in that post that with a bit of fiddling supposedly will validate dates in regex, but it is a scary looking regex

Community
  • 1
  • 1
gwillie
  • 1,893
  • 1
  • 12
  • 14
  • Can you explain as to why you're using ?: it would work otherwise as well, as far as I can tell. – xyz Jun 25 '15 at 14:20
3

Try

^\d{4}[-/.]\d{1,2}[-/.]\d{1,2}$

The curly braces {} give the number allowed. E.g., \d{1,2} means either one or two digits.

cxw
  • 16,685
  • 2
  • 45
  • 81
  • @Michael Happy to help! In case you haven't seen it, [this](http://www.regular-expressions.info/dotnet.html) site is a reference I rely on for regex details. – cxw Oct 16 '13 at 11:32
0

You may need more than that to match date. Try this:

(19|20)\d\d([-/.])(0?[1-9]|1[012])\2(0?[1-9]|[12][0-9]|3[01])
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
bansi
  • 55,591
  • 6
  • 41
  • 52
0
((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))\-((0[13578])|(1[02]))\-((0[1-9])|([12][0-9])|(3[01])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))\-((0[469])|11)\-((0[1-9])|([12][0-9])|(30)))|(((000[48])|([0-9][0-9](([13579][26])|([2468][048])))|([0-9][1-9][02468][048])|([1-9][0-9][02468][048]))\-02\-((0[1-9])|([12][0-9])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))\-02\-((0[1-9])|([1][0-9])|([2][0-8])))

This is the regex for yyyy-MM-dd format.

You can replace - with \/ for yyyy/MM/dd...

Tested working perfect..

falsarella
  • 12,217
  • 9
  • 69
  • 115
Ajit
  • 1
  • 9
    This hurts my eyes. ;) – woot May 28 '14 at 03:59
  • 1
    Doesn't work for 1900-02-29 and 2100-02-29 which are not valid dates. Also, you could replace [0-9] with \d making your regex expression much shorter – Graham Apr 07 '15 at 12:19
0

Ajit's regex is nearer to perfect but leaks the evaluation of the leap years that end with 12 and 16. Here is the correction to be just perfect

((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-((0[13578])|(1[02]))-((0[1-9])|([12][0-9])|(3[01])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-((0[469])|11)-((0[1-9])|([12][0-9])|(30)))|(((000[48])|([0-9]0-9)|([0-9][1-9][02468][048])|([1-9][0-9][02468][048])|([0-9]0-9)|([0-9][1-9][13579][26])|([1-9][0-9][13579][26]))-02-((0[1-9])|([12][0-9])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-02-((0[1-9])|([1][0-9])|([2][0-8])))
Ahmed Wahbi
  • 128
  • 1
  • 2
  • 10
  • Doesn't work for 1900-02-29 and 2100-02-29 which are not valid dates. Also, you could replace [0-9] with \d making your regex expression much shorter – Graham Apr 07 '15 at 12:21
-1

Try this. This accepts all four patterns

@"\d{4}[- /.]([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])"
Akshit Zaveri
  • 4,166
  • 6
  • 30
  • 59