I'm trying to write a regular expression that validates a date. The regex needs to match the following formats
- MM-DD-YYYY
- MM/DD/YYYY
- DD-MM-YYYY
- DD/MM/YYYY
- YYYY-MM-DD
- YYYY/MM/DD
- YYYY-DD-MM
- YYYY/DD/MM
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.