anyone good at regular expressions? EDIT: Need some more advanced help

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
I need to match the following format:

03/04/2004 14:23:00
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Here's a sample that doesn't actually validate the date and time itself. Try looking at Regexlib for more complete samples. You'll often find that in a regex you can make assumptions based on your knowledge of the data, so the following might very well suffice:

(?:\d{2}/){2}\d{4} (?:\d{2}:?){3}

That's just quick and dirty, but it should accommodate your needs.
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
It's for a validator control on an ASP.Net page.
I want to ensure that the value in a text box matches that date time format.

Does this look right:

\d{2}/\d{2}/\d{4} +\d{2}:\d{2}:\d{2}

I wasn't sure if I was handling the space in between the date and time correctly.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Yes, yours will work fine as well. Your are allowing for at least one space, whereas my example allowed for only one.
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
Thanks.
I haven't really done much with regex's, but wow, looking at that link, you can get pretty detailed.

I guess I should probably make sure it's a real date as well. Although it would be so much simpler to do just do a try catch with a conversion to date.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
If this data is from user input, then I would absolutely verify that it is a valid date; however, if not, it's usually not necessary. By the looks of your sample data it looks as though it's probably coming from an outside system (e.g. accounting perhaps?) or something, and in which case you can generally assume that the data is within the appropriate constraints.
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
The text boxes will be populated with a default value, but the users will be able to change the values so I will be checking for valid dates. Actually, I may just trap any errors that occur at the time I set the stored procedure parameters with the input date values.

Thanks.
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
I know it's a little twisted, but I'm actually having fun figuring out this stuff.
So I decided to try to do all the validation with the regex rather than in my other code.

Again, I want to exactly match this format and no others while ensuring that the date is actaully a valid date:
03/09/2004 19:32:18

Here is what I have so far (I broke it into the pieces so it's easier to read):

(0[469]|11)/(0[1-9]|1[0-9]|2[0-9]|30)/(19|20)[0-9][0-9] 'all the 30 day months
(0[135789]|1[02])/(0[1-9]|1[0-9]|2[0-9]|3[01])/(19|20)\d{2} 'all the 31 day months
(02)/ 'February



([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] 'Time section

I'm confused though about how to capture the year and use it to determine whether or not February 29 is a valid date.
I could just make it always valid, but I'd rather not do that.