tags:

views:

47

answers:

5

Hi does any one know a reg ex for a uk date format e.g. dd/mm/yyyy.

The dd or mm can be 1 character e.g. 1/1/2010 but the year must always be 4 characters.

Thanks in advance

+6  A: 
^\d{1,2}/\d{1,2}/\d{4}$

will match 1/1/2000, 07/05/1999, but also 99/77/8765.

So if you want to do some rudimentary plausibility checking, you need

^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/\d{4}$

This will still match 31/02/9999, so if you want to catch those, it's getting hairier:

^(?:(?:[12][0-9]|0?[1-9])/0?2|(?:30|[12][0-9]|0?[1-9])/(?:0?[469]|11)|(?:3[01]|[12][0-9]|0?[1-9])/(?:0?[13578]|1[02]))/\d{4}$

But this still won't catch leap years. So, modifying a beast of a regex from regexlib.com:

^(?:(?:(?:(?:31\/(?:0?[13578]|1[02]))|(?:(?:29|30)\/(?:0?[13-9]|1[0-2])))\/(?:1[6-9]|[2-9]\d)\d{2})|(?:29\/0?2\/(?:(?:(1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:0?[1-9]|1\d|2[0-8])\/(?:(?:0?[1-9])|(?:1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$

will match

1/1/2001
31/5/2010
29/02/2000
29/2/2400
23/5/1671
01/1/9000

and fail

31/2/2000
31/6/1800
12/12/90
29/2/2100
33/3/3333

All in all, regular expressions may be able to match dates; validating them is not their forte, but if they are all you can use, it's certainly possible. But looks horrifying :)

Tim Pietzcker
It's usually better, once you've found a likely match, to stuff it into a variable of type date (on whatever the platform), and let the platform worry about leap year rules, etc.
Damien_The_Unbeliever
The first 0 isn't optional, suffix it with a `?`
Andrew Dunn
Either keep it simple, or search the web for an expression that handles everything including leap years. There isn't really any point to make a more complicated expression if it still can't handle all cases.
Guffa
@Andrew Dunn: Thanks, I had missed that and have corrected it.
Tim Pietzcker
@Damien: I absolutely agree.
Tim Pietzcker
@Guffa: You're right, and I was - just took a while to change the monster regex to his exact specifications...
Tim Pietzcker
+1  A: 
\b(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/](19|20)?[0-9]{2}\b

Match :

  • 1/1/2010
  • 01/01/2010

But also invalid dates such as February 31st

madgnome
I think matching correct days in a month is useless when not matching february and leap years ;) Besides that, it is a difference between matching the form and matching the values.
Caspar Kleijne
A: 

^\d{1,2}/\d{1,2}/\d{4}$

In braces there is min and max char count. \d means digit, ^ start, and $ end of string.

Michał Niklas
+3  A: 

Regex is not the right tool for this job.

It is very difficult (but possible) to come up with the regex to match a valid date. Things like ensuring Feb has 29 days on leap year and stuff is not easily doable in regex.

Instead check if your language library provides any function for validating dates.

PHP has one such function called checkdate :

bool checkdate  ( int $month  , int $day  , int $year)
codaddict
A: 
\b(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d

This wont validate the date, but you can check for format

regexhacks