3
I was trying to validate leap years using regex in C++.
If the user type 28/02/1900
, it would return valid. But if the same type 29/02/1900
, he 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)))\1|(?:(?:29|30)(/)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(/)(?:0?2|(?:Feb))\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]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?: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:
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 type more than 29 days for February and 29 days for a non-leap year, it should not validate.
If anyone can help me I’d be grateful.
Do you need through regular expressions to do that account? It is not better to do a simple account no?
– Jefferson Quesado
yes he wanted the calculation to be done only by regex validating only by it...
– dark777
I’m with @Jeffersonquesado. Sounds like a cannon to kill a fly
– Isac
Why do you want to validate if a date is leap using regular expressions? This seems to be a XY problem.
– Victor Stafusa
@Isac is not a cannon to kill fly. It is an electric weld to kill a fly. Namely: A tool totally unsuitable for work.
– Victor Stafusa
@Victorstafusa Yes it would be more correct to see it that way
– Isac
It is possible, but it will be long and boring. I will do over the definition: multiple of 400, or else multiple of 4 but not 100
– Jefferson Quesado
@Jeffersonquesado Doing mathematics with regular expressions is a terrible thing.
– Victor Stafusa
i have programs in c++ that validates but wanted to learn and understand how to do only by regex as it would be because if I am not mistaken in other languages I think I would have but my focus is to do it using c++...
– dark777
@Victorstafusa I know, so the part of "extensive and boring@. Could by tedious too
– Jefferson Quesado
@dark77 Learning to use regex also concerns knowing what kind of work they are not able to do or are a bad choice. Regex is not a good answer to this problem.
– Victor Stafusa
yes is a bad choice but it would be really to understand and learn how would be using only the same...
– dark777
@dark777 I understand what you say about trying to learn. My view is that this is so far from the real context of a regex that it does not ultimately learn much, because much of it will not be applicable in other contexts. I think it becomes much more productive and easy to learn from other realistic and useful examples of regex
– Isac
https://stackoverflow.com/questions/24319295/date-leap-year-validation-regex this can help you.
– CypherPotato
@dark777 I replied. Much more mathematics than regex. There is no escape. Look for another alternative to study regex in a less mathematical way, such as recognizing emails
– Jefferson Quesado
I’ve done a regex to validate emails until I created a theme here and the regex I found searching on google was absurdly big full of things that in my opinion were unnecessary so I was moving until shortening and validate in the formats I needed was show ball... This is the topic: https://answall.com/questions/182756/express%C3%a3o-regular-para-e-mail-em-c
– dark777