Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Can you help me write the correct regular expression?
I'm stumped. I'm trying to do date validation with regular expressions. So far, I haven't found any examples that are complete.
I can articulate the rules, I just can't come up with the correct regular expression. I'm trying to validate against the mm/dd format.
mm can be 0-12, with or without a leading zero ( ^0?[1-9]) | 1[0-2] )
but, depending on what the month is, we want to make sure that we only validate actual date numbers (January, March, May, July, August, October, and December = 31; February = 29; April, June, September, and November = 30).
So I think I have to have three all-encompassing OR statements in my RegEx.
I've been trying to come up with a string that will do this (the days also could have a leading zero or not), and have the RegEx validate ONLY actual dates (4/31, 2/30, 6/00, 14/02 would not validate, but 1/1, 1/11, 1/01, 5/15 would).
Has anyone done this? Surely they have. Surely I don't have to reinvent the wheel.
(And for all you Airplane fans..."Don't call me Shirley.")
Thanks in advance!
4 Answers
- 1 decade agoFavorite Answer
I'm not sure about that, seems like you want too much from a regular expression. Maybe it is possible but I don't see how you are getting past the day issue (especially when you consider leap years).
This website has a nice tutorial of a regular expression to check the format and then uses an if to check validity.
- deonejuanLv 71 decade ago
You will have to use back reference to capture 4 digits for a year, 2 or 1 digits for a month, 1 through 31 for a day.
To add the logic for <29 if not a leap but is a 02 month you will have to program. perl was made for RegEx and RegEx was made for perl.
- PfoLv 71 decade ago
Why not parse and validate the format first, then determine if the date is valid? It would be easier than doing it all in regex.