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! |