-1

I've been trying unsuccessfully to rewrite this regular expression to use the dd-mm-yyyy 29-02-2020 format and also use ./- as separators. Can you please help?

^(?:\d{4}\/(?:(?:(?:(?:0[13578]|1[02])\/(?:0[1-9]|[1-2][0-9]|3[01]))|(?:(?:0[469]|11)\/(?:0[1-9]|[1-2][0-9]|30))|(?:02\/(?:0[1-9]|1[0-9]|2[0-8]))))|(?:(?:\d{2}(?:0[48]|[2468][048]|[13579][26]))|(?:(?:[02468][048])|[13579][26])00)\/02\/29)$
Norman
  • 5,685
  • 19
  • 74
  • 128

1 Answers1

-1

I dont know if this is legal, or maybe (for sure) it is not the best solution. but it works.

(^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$|^([0-2][0-9]|(3)[0-1])(\.)(((0)[0-9])|((1)[0-2]))(\.)\d{4}$|^([0-2][0-9]|(3)[0-1])(\-)(((0)[0-9])|((1)[0-2]))(\-)\d{4}$)

enter image description here

Freddy
  • 470
  • 1
  • 5
  • 14
  • Well, for you `30/02/2020` is a valid date!!!!! And why all these capture groups that slown down the process? – Toto Feb 21 '20 at 15:06
  • it's a better approach validate that behaviour in code instead of doing a regex. – Freddy Feb 21 '20 at 15:20
  • True, but why are you giving a wrong answer? – Toto Feb 21 '20 at 15:22
  • My bad, sorry. I did not notice that was a invalid date when I took the screenshoots. It's not a perfect solution (solutions involving ReEx will hardly be). A solution for what would be something like this ^(((0[1-9]|[12]\d|3[01])\-(0[13578]|1[02])\-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\-(0[13456789]|1[012])\-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\-02\-((19|[2-9]\d)\d{2}))|(29\-02\-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$ I would prrefer validate feb by code that having this moster... ... which only handle "-" divisor – Freddy Feb 21 '20 at 15:28