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

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find RFC 2822 Date Regex
Expression
^(?:\s*(Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s*)?(0?[1-9]|[1-2][0-9]|3[01])\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(19[0-9]{2}|[2-9][0-9]{3}|[0-9]{2})\s+(2[0-3]|[0-1][0-9]):([0-5][0-9])(?::(60|[0-5][0-9]))?\s+([-\+][0-9]{2}[0-5][0-9]|(?:UT|GMT|(?:E|C|M|P)(?:ST|DT)|[A-IK-Z]))(\s*\((\\\(|\\\)|(?<=[^\\])\((?<C>)|(?<=[^\\])\)(?<-C>)|[^\(\)]*)*(?(C)(?!))\))*\s*$
Description
This is the best RFC 2822 ( http://www.faqs.org/rfcs/rfc2822 ) date format regular expression I could come up with. I've tested it, not very extensively though. This regex also validates obsolete standard, excluding comments anywhere.
Matches
Thu, 6 Jan 2005 18:44:56 -0500 | Thu, 21 Dec 2000 16:01:07 +0200 (\(proper\) & (nested) comment)
Non-Matches
Thu, 0 Jan 1856 18: 44:56 -0500 | Thu, 21 Dec 2000 16:01:07 +0200 (\(improper) comment) | blah
Author Rating: The rating for this expression. Ivik Injerd
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: Enhanced
Name: Ivik
Date: 7/19/2007 1:44:55 AM
Comment:
Thank you Doekman! I went ahead and modified what you provided to not match day 0 and match 2 digit years and nested comments (after much research and testing). Please tell me if you find any problems. Enjoy! ^_^


Title: Enhancement
Name: Doekman
Date: 7/11/2007 4:31:21 PM
Comment:
I've enhanced the regex. The day can now also start with a zero (02), and I've added some non-capturing "?:" markers. Math: for the record; it already matched "Sun, 30 Jan 2005 05:01:28 GMT". The new RegExp is: /^(?:(Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s+)?(0[1-9]|[1-2]?[0-9]|3[01])\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(19[0-9]{2}|[2-9][0-9]{3})\s+(2[0-3]|[0-1][0-9]):([0-5][0-9])(?::(60|[0-5][0-9]))?\s+([-\+][0-9]{2}[0-5][0-9]|(?:UT|GMT|(?:E|C|M|P)(?:ST|DT)|[A-IK-Z]))(\s+|\(([^\(\)]+|\\\(|\\\))*\))*$/


Title: Fixed
Name: Ivik
Date: 4/30/2005 4:30:26 PM
Comment:
I knew it wasn't perfect... it should work now. Thanks!


Title: You fail on a common format
Name: Matt
Date: 4/28/2005 3:35:25 AM
Comment:
Your Regex doesn't even parse "Sun, 30 Jan 2005 05:01:28 GMT" Lame.


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