I'm trying to write a regular expression that validates a date. The regex needs to match the following formats
and should not match formats like DD-MM/YYYY.
I have tried this Regex
'^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$'
but I am only able to extract the dates with mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy format.
I also tried using this (?:\\d{2}){1,2}[-|/|.]\\d{2}-|/|.{1,2} where I am getting the dates but I am also getting the values like 5542-21-54, 99/01/2019 which does not represent the date.
Thanks, everyone for the help.
If you would like to match days yo need to do this regex:
((1|2)[0-9]|3[0-1]|0?[1-9])
For the month you need:
(0?[1-9]|1[0-2])
For the year you need (only 1900 and 2000, for the 3000 you need maybe to update your script when we will arrive in 2999):
((19|20)?[0-9]{2})
After you have different delimiter and date orders, so:
((1|2)[0-9]|3[0-1]|0?[1-9])\/(0?[1-9]|1[0-2])\/((19|20)?[0-9]{2}) # for DD/MM/YYYY
((1|2)[0-9]|3[0-1]|0?[1-9])\-(0?[1-9]|1[0-2])\-((19|20)?[0-9]{2}) # for DD-MM-YYYY
((19|20)?[0-9]{2})\/((1|2)[0-9]|3[0-1]|0?[1-9])\/(0?[1-9]|1[0-2]) # for YYYY/DD/MM
((19|20)?[0-9]{2})\-((1|2)[0-9]|3[0-1]|0?[1-9])\-(0?[1-9]|1[0-2]) # for YYYY-DD-MM
...
Finally, you need to combine them and have a single match with (expression1|expression2|...)
:
\W(((1|2)[0-9]|3[0-1]|0?[1-9])\/(0?[1-9]|1[0-2])\/((19|20)?[0-9]{2})|((1|2)[0-9]|3[0-1]|0?[1-9])\-(0?[1-9]|1[0-2])\-((19|20)?[0-9]{2})|((19|20)?[0-9]{2})\/((1|2)[0-9]|3[0-1]|0?[1-9])\/(0?[1-9]|1[0-2])|((19|20)?[0-9]{2})\-((1|2)[0-9]|3[0-1]|0?[1-9])\-(0?[1-9]|1[0-2]))\W
I need the \\W
at the begining and the end of regex. this one check if you have anything (\\s, \\;, \\,, etc.).
You need to complete the missing combinations.
Enjoy ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.