I modified a regex I got here. I needed to change it because I needed it to match the following additional criteria:
- Dates with only Month and Year
- Full dates in the form mm dd, yyyy
- Dates with year only
- Input with extraneous info (like Vol. 51, No. 1, Mar 2008)
This is what I have so far. I did this with RegexBuddy to help me parse the logic, but it's so complex I'm not certain I have the most efficient solution.
\b(?:((Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)|((((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?) 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?) (0?[1-9]|([12]\d)|30))|(Feb(ruary)? (0?[1-9]|1\d|2[0-8]|(29(?=, ((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))))),)) ((1[6-9]|[2-9]\d)\d{2}))|((1[6-9]|[2-9]\d)\d{2})
Is there anything that could be done to preserve the functionality of both the original regex and my additional criteria?
Here is the code where I implement this, if it helps you see what I'm trying to do. The output of the parseDate function is supposed to be a string date in the form "yyyy mm dd" (i.e., example 4 should output "2008 Mar"):
//generalized RegEx function
function returnRegExMatch(ex,haystack) {
var needle = ex.exec(haystack);
if (needle) { return needle[0]; }
}
// date extraction (uses returnRegExMatch)
function parseDate(date) {
//strip anything other than a valid date
var dateRe = /\b(?:((Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)|((((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?) 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?) (0?[1-9]|([12]\d)|30))|(Feb(ruary)? (0?[1-9]|1\d|2[0-8]|(29(?=, ((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))))),)) ((1[6-9]|[2-9]\d)\d{2}))|((1[6-9]|[2-9]\d)\d{2})/;
date = returnRegExMatch(dateRe,date);
var yearRe = /[0-9][0-9][0-9][0-9]/;
var monthRe = /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/;
var dayRe = /[0-9]?[0-9],/;
var year = returnRegExMatch(yearRe,date);
var month = returnRegExMatch(monthRe,date);
var day = parseInt(returnRegExMatch(dayRe,date),10);
var dateReturned = "";
if (year) { dateReturned = year; }
if (month) { dateReturned = dateReturned + " " + month; }
if (month && day) { dateReturned = dateReturned + " " + day; }
return dateReturned;
}
Thanks!
EDIT Thanks to all who took time to respond. You guys did what I was hoping for, pointing out the most ridiculous things in my implementation. I decided to simplify the main regex quite a bit. Here's the result:
\b(?:(?:Jan(?:uary)?|Feb(?:ruary)?|Ma(?:r(?:ch)?|y)|Apr(?:il)?|Ju(?:(?:ly?)|(?:ne?))|Aug(?:ust)?|Oct(?:ober)?|(?:Sept|Nov|Dec)(?:ember)?) (?:\d{1,2}, )?)?\d{4}
This doesn't worry about detect invalid dates based on leap years or whatever. @Bart convinced me that this is probably best done with native JS than regex. Thanks to @Tim too for pointing out the need for non-capturing parentheses.
If anyone has further suggestions for how I should refine this regex please fire away.