How can I write a regex for MM:DD:YYYY:HH:MM:SS?
Asked
Active
Viewed 3,171 times
2 Answers
2
If you want to capture the values for month, year, ... you could use something like this, I suppose :
([0-9]{2}):([0-9]{2}):([0-9]{4}):([0-9]{2}):([0-9]{2}):([0-9]{2})
If supported by your regex engine, \d
can be used as an alias to [0-9]
:
(\d{2}):(\d{2}):(\d{4}):(\d{2}):(\d{2}):(\d{2})

Pascal MARTIN
- 395,085
- 80
- 655
- 663
1
It depends whether you want to check are all characters divided by :
numbers or you also want to check whether you will not get totaly improper data like the 32nd of December or the hour like 25:66. To have such a basic validation you should use a little more complex regexp than the one provided by @Pascal MARTIN, e.g.,
[0-1][0-9]:[0-3][0-9]:[1-2][0-9]{3}:[0-2][0-9]:[0-5][0-9]:[0-5][0-9]

MPękalski
- 6,873
- 4
- 26
- 36
-
1I totally support you on this answer because you've addressed the big issue left completely untouched by the other solution (nothing against other author, of course.) This isn't perfect either, but it's documented. – Guttsy Apr 06 '11 at 19:13