tags:

views:

234

answers:

5

I'll admit I'm rubbish at regular expressions and find some of the tools out there that are supposed to make generating them easier painfully difficult to use. That's my fault granted.. anyway..

Can anyone come up with a single regular expression that will allow both dd/MM/yyyy and/or dd/MM/yyyy hh:mm:ss

Yes, I've Googled for it and searched regexlib.

It would be much appreciated.

Cheers in advance.

UPDATE :

Guys, I obviously considered server side parsing.. I am asking specifically for a regex. I may still fall back on this.

I have also looked at lots of online material including sites that a quick Google brings up, so please don't submit obvious sites as i've tried them and resorted to asking the question here.

UPDATE :

Guys, I'm leaning towards doing it via server side date parsing.. But I would still be curious what the regex would be.

+1  A: 

I suggest you to take a look at Regular Expression Matching a Valid Date, Matching a Date in mm/dd/yyyy Format and at RegexLib - Dates and Times.

Nathan Campos
+2  A: 

Here is one

[0-9]{2}/[0-9]{2}/[0-9]{4}( [0-9]{2}:[0-9]{2}:[0-9]{2})?

Edit: improved formatting, thanks Nathan Campos.

Or, eliminating many more invalid values:

[0-3][0-9]/[01][0-9]/[0-2][0-9]{3}( [0-2][0-9]:[0-5][0-9]:[0-5][0-9])?
Marcel Gosselin
Shouldn't the space be represented by \s as in[0-9]{2}/[0-9]{2}/[0-9]{4}(\s[0-9]{2}:[0-9]{2}:[0-9]{2})?
RMcLeod
Not quite. as this allows 99/12/2009 12:00:03. Can we limit it to valid days and months
Lee Englestone
Lee, regex are really a good tool to use to parse text but using it to parse valid dates and times is going to drive you nuts. The regular expression will be kilometres long to make sure you have all cases correct. As Joel has already pointed out, `DateTime.TryParseExact()` is what you need.
Marcel Gosselin
+4  A: 

You're probably better off using DateTime.TryParseExact() for this. It has an overload that allows you to specify multiple possible formats.

Joel Coehoorn
I may resort to this but am curious to find a regex solution.
Lee Englestone
This is correct. Unless you resort to an absurd regex that is difficult to maintain, your regex will parse `31/02/2009` while `DateTime.TryParseExact` will not. And then add a more tricky example like `29/02/1900` versus `29/02/2000` and you'll realize you should just let `DateTime.TryParseExact` do the hard work for you.
Jason
Even though this doesn't answer the regex question, it is the correct way of validating the dates in the 2 formats I require. Hence why I have marked it as the answer.
Lee Englestone
A: 

You can try out the different suggestions here: http://www.regexr.com/

mga
A: 

This should work fine, and does most validation (except for number of days in months):

([0-2][0-9]|3[0-1])/(0[1-9]|1[0-2])/[0-9]{4}( ([0-1][0-9]|2[0-3])(:[0-5][0-9]){2})?

You could add month-wise intelligence to the days, but the RegEx expression for that would look rather hairy.

Mike Hanson
hmm. This seems to allow : 12/12/2009 42:00:00
Lee Englestone
Nope. See ([0-1][0-9]|2[0-3]). The first option is 0 or 1, followed by 0-9. The second option is 2, followed by 0-3. BTW, I'm using <a href="http://www.regexbuddy.com>RegexBuddy</a> to help me through this stuff. I highly recommend it!
Mike Hanson