I was trying to validate leap years using regex in C ++.
If the user types 28/02/1900
, it would return valid. But if the same type 29/02/1900
, it would return error.
Searching, I found this regex that compiled but is not validating.
const std::regex pattern("^(?:(?:31(/)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))|(?:(?:29|30)(/)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(/)(?:0?2|(?:Feb))(?:(?:(?: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]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))(?:(?:1[6-9]|[2-9]\d)?\d{2})$");
I tried to simplify based on another regex for something like:
const std::regex pattern("(0[1-9]|1[0-9]|2[8|9])[*-. /](0[2]|Feb)[*-. /](19[04|08|12|16])\d{2,2}"); //dd/mm/yyyy
I simplified from this based on the first one:
const std::regex pattern("(0[1-9]|1[0-9]|2[8|9]|3[0|1])[*-. /](0[1-9]|1[0|1|2]|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[*-. /](19|20)\d{2,2}"); //dd/mm/yyyy
To conclude I am trying to write a regex that reads both but if the person types more than 29 days for February and 29 days for a non-leap year, it should not validate.
If anyone could help me, I would be grateful.