I'm new to regex and need one to accept (and validate the date) only the dd-mm-yyyy format, with leading zeros for single-digit days or months and only hyphens. I have been able to modify the code given by Ofir Luzon's response here to only allow hyphens but am not able to completely achieve the leading zeros part. Also searched a lot of regex libraries but most solutions either do not validate dates or they allow single digits in the dd and mm parts. Here's the 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})$
So this accepts dd-mm-yyyy, d-mm-yyyy and dd-m-yyyy, dd-mm-yy, d-m-yy,dd-m-yy, d-mm-yy and validates date, but how to make it restrict to exactly two digits for dd and mm AND four digits for yyyy?
I'm able to understand that the quantifiers for previous tokens need to be changed but am not able to figure out how.