Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx Questions 1

Status
Not open for further replies.

ietprofessional

Programmer
Apr 1, 2004
267
US
Hi Everyone,

I couldn't find a regex forum so I'm trusting my fellow asp.net-er will have this type of knowledge.

I'm trying to validate a date field before I put it into a database. Does anyone have a regex for dates formatted in all of these ways 3/3/2005, 03/3/2005, 3/03/2005 and 03/03/2005.

Thanks!!!!!
 
I know I did not make this one. Probably from
validationexpression="((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(29)([/])([3579][26]00)$)|(^(0?2)([/])(29)([/])([1][89][0][48])$)|(^(0?2)([/])(29)([/])([2-9][0-9][0][48])$)|(^(0?2)([/])(29)([/])([1][89][2468][048])$)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048])$)|(^(0?2)([/])(29)([/])([1][89][13579][26])$)|(^(0?2)([/])(29)([/])([2-9][0-9][13579][26])$))"

Marty
 
The regex is straight forward, where this person got really sharp is figuring out leap years. I'll break down the first one. If you remember than ^ is the beginning of the string, $ is the end of the string, ? means optional(zero or one), | means OR, and () is a group, [] contain an explicit set of characters to match, {} holds the length(s) and \ is an ecscape character here it is \d which means digit it's easy to follow.
The first one is looking for dates that can have 31 days.

((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)

It's matching the beginning to the end of the string with the ^ and the $
first group (10|12|0?[13578])
10 or 12 or an optional 0 infront of 1,3,5,7,8.
second group ([/]) /
third group (3[01]|[12][0-9]|0?[1-9])
3 followed by a 0 or 1
or
1 or 2 followed by 0,1,2,3,4,5,6,7,8,9
or an optional 0 followed by 1,2,3,4,5,6,7,8,9
fourth group ([/]) /
fifth group ((1[8-9]\d{2})|([2-9]\d{3}))
1 followed by 8 or 9 followed by two digits
or 2,3,4,5,6,7,8,9 followed by three digits.

That's as much as I have ever looked at it, if you ever write some Perl you'll see some regex's that will blow your mind.

Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top