RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Flexible ISO -> US -> EU Timestamp or Date
Expression
^(?:(?:(?:(?:[1-2][0-9]{3}) *(?:[\/\-\., ]) *(?:1[0-2]|0?[1-9]) *(?:[\/\-\., ]) *(?:[12][0-9]|3[01]|0?[1-9]))|(?:(?:1[0-2]|0?[1-9]) *(?:[\/\-\., ]) *(?:[12][0-9]|3[01]|0?[1-9]) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3})))|(?:(?:[12][0-9]|3[01]|0?[1-9]) *(?:[\/\-\., ]) *(?:1[0-2]|0?[1-9]) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3})))|(?:(?:(?i:(?:j(?:an(?:uary)?|u(?:ne?|ly?)))|a(?:pr(?:il)?|ug(?:ust)?)|ma(?:y|r(?:ch)?)|(?:nov|dec)(?:ember)?|feb(?:ruary)?|sep(?:tember)?|oct(?:ober)?)) *(?:[\/\-\., ]) *(?:(?:[12][0-9]|3[01]|0?[1-9])|(?:(?i:[23]?1st|2?2nd|2?3rd|[4-9]th|1[0-9]th|20th|2[4-9]th|30th))) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3})))|(?:(?:(?:[12][0-9]|3[01]|0?[1-9])|(?:(?i:[23]?1st|2?2nd|2?3rd|[4-9]th|1[0-9]th|20th|2[4-9]th|30th))) *(?:[\/\-\., ]) *(?:(?i:(?:j(?:an(?:uary)?|u(?:ne?|ly?)))|a(?:pr(?:il)?|ug(?:ust)?)|ma(?:y|r(?:ch)?)|(?:nov|dec)(?:ember)?|feb(?:ruary)?|sep(?:tember)?|oct(?:ober)?)) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3}))))|(?:(?:(?:(?:[1-2][0-9]{3}) *(?:[\/\-\., ]) *(?:1[0-2]|0?[1-9]) *(?:[\/\-\., ]) *(?:[12][0-9]|3[01]|0?[1-9]))|(?:(?:1[0-2]|0?[1-9]) *(?:[\/\-\., ]) *(?:[12][0-9]|3[01]|0?[1-9]) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3})))|(?:(?:[12][0-9]|3[01]|0?[1-9]) *(?:[\/\-\., ]) *(?:1[0-2]|0?[1-9]) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3})))|(?:(?:(?i:(?:j(?:an(?:uary)?|u(?:ne?|ly?)))|a(?:pr(?:il)?|ug(?:ust)?)|ma(?:y|r(?:ch)?)|(?:nov|dec)(?:ember)?|feb(?:ruary)?|sep(?:tember)?|oct(?:ober)?)) *(?:[\/\-\., ]) *(?:(?:[12][0-9]|3[01]|0?[1-9])|(?:(?i:[23]?1st|2?2nd|2?3rd|[4-9]th|1[0-9]th|20th|2[4-9]th|30th))) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3})))|(?:(?:(?:[12][0-9]|3[01]|0?[1-9])|(?:(?i:[23]?1st|2?2nd|2?3rd|[4-9]th|1[0-9]th|20th|2[4-9]th|30th))) *(?:[\/\-\., ]) *(?:(?i:(?:j(?:an(?:uary)?|u(?:ne?|ly?)))|a(?:pr(?:il)?|ug(?:ust)?)|ma(?:y|r(?:ch)?)|(?:nov|dec)(?:ember)?|feb(?:ruary)?|sep(?:tember)?|oct(?:ober)?)) *(?:[\/\-\., ]) *(?:(?:[0-9]{1,2})|(?:[1-2][0-9]{3})))) *(?:(?:(?:1[0-2]|0?[1-9])(?: *(?:\:) *(?:[1-5][0-9]|0?[0-9]))?(?: *(?:\:) *(?:[1-5][0-9]|0?[0-9]))? *(?:(?i:[ap]m)))|(?:(?:2[0-3]|[01]?[0-9])(?: *(?:\:) *(?:[1-5][0-9]|0?[0-9]))(?: *(?:\:) *(?:[1-5][0-9]|0?[0-9]))?))))$
Description
Updated: 5-15-2007 @ 2:30pm --- This expression matches all kinds of possible date or timestamp formats. No it dose not validate for leap years or months where the 31st day is not valid. I use it in my code so I can let my site users format a date in any way they please. I am basing it loosely on ISO, US and European standards found. The reason why it works for me is that I use it along with some code I made that extracts the year, month, day, hour, minute and seconds from the string and use it to create a valid date in my code. This is why I don't care about leap year/Feb rules since, once I got the date parts I can validate for these variations within the language I'm using very easily without making my expression 10,000 chars long. Although that would be an awesome thing to see and if anybody can make it as flexible as this one is and check for leap/Feb rules I would love to see it. I did not want to add anything to my UI to educate my user on how I expect them to format it. The expression assumes that the date is ISO then US and last European standard. This may not be good for some people but for me I think that if it is not a US date but it’s a valid European date than Ill just take it since where I use this code is not that critical. I just want flexibility and ease of use for my users. Oh, and I really did not think of performance hit due to the length of the pattern. I ran it. It worked fast for me. It’s only for a small text string. I tested it in PHP and The Regex Coach (The coach was in pain afterward, hee hee). I hope I brake a record with this bad boy. This is the longest expression I have ever seen! Its 2,214 chars long, WOO HOO!
Matches
1999-12-31 5:30 pm | 1-1-1999 22:30:25 | 01/01/99 | Feb .01.99 | Feb 1, 99 | 01 feb 1999 | Jan 1st 99
Non-Matches
13/31/99 | 12/32/99 | 1999-31-12 | 1st of Jan 1999 | first of jan 1999
Author Rating: Not yet rated. Noel Rivera
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: longest
Name: long
Date: 5/22/2007 7:10:04 PM
Comment:
here's the longest i've seen :p http://www.regular-expressions.info/email.html go to the bottom of that page :o)


Title: Stricter regex
Name: Michael Ash
Date: 5/16/2007 5:23:23 PM
Comment:
Since you asked. Here's a regex that check most of the same formats as your but includes days and leap year checks. http://regexlib.com/REDetails.aspx?regexp_id=1784 Some slight differences. Years must be 4 digits. Can't check leap years otherwise. Written in .Net flavor just cause it was easier. MMM dd, yyyy format must include a comma. I'm sure I'm forgetting something. Anyway it's just for show. I wouldn't use it for real because it's ambiguous in some cases if you accept US and UK formats. ex 12-7-1941 = Dec 7 or July 12? Depends on where you are.


Title: Doesn't handle Feb
Name: Michael Ash
Date: 5/15/2007 10:28:05 AM
Comment:
Doesn't handle day in Feb or Leap years correctly. And sorry longer regexes have been written.


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials