Regular expression for dates without tab

Asked

Viewed 3,283 times

3

I need to know if a sequence of 8 numbers is a valid date in the dd/mm/yyyy format. The closest I came was using this expression:

/^[0-3]{1}\d{1}[0-1]{1}[0-9]{1}[1-2]{1}\d{3}$/gm;

But it is returning true to sequences that are not valid dates as for example:

39091991 (39/09/1991)
10181991 (10/18/1991)

I would also like the year to have a limit, 1990 - 2020.

I’m not much with regular expressions, so if anyone can help me.

  • 3

    The problem is that regular expressions do not understand that values can only go from X to Y, as in the case of days, which vary with the month (makes the problem even worse!) and with the year (for February). What can be done is validate the date after capturing it.

  • 3

    In fact, you are demanding of regular expressions much more than this tool is capable of... How about using regex only to eliminate "obvious" cases (i.e. accept false positives, just don’t let it have false negatives) then doing a more rigorous validation, using the calendar functions of your platform?

  • If possible, follow is my late solution, validating months of 30 and 31 days, including 29 February for leap years.

3 answers

4


Try this

(0[1-9]|[1-2][0-9]|3[0-1])(0[1-9]|1[0-2])(199[0-9]|200[0-9]|201[0-9]|2020)

Being

Dias
(0[1-9]|[1-2][0-9]|3[0-1])

Mes
(0[1-9]|1[0-2])

Ano
(199[0-9]|200[0-9]|201[0-9]|2020)

Only problem is that it does not validate February (30.02.2000) for example is valid.

Have to validate the dates with this Exp, but there is no year limitation:

(?:(?:(?:[01][1-9]|2[1-8])(?:0[1-9]|1[0-2])|(?:29|30)(?:0[13-9]|1[0-2])|31(?:0[13578]|1[02]))[1-9]\d{3}|2902(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00))
  • Postei this solution treating this one problem cited by you in your reply, for not treating February.

4

Although @Adirkuhn has already provided an acceptable solution to AP. After seeing this I went looking for something more powerful, that could at least validate months with 30 and 31 days, because I knew that this was possible, despite knowing that it was laborious.

Then I found that answer in Soen, which was beyond my expectations, as it is valid until dates for leap years (in this case the 29th of February).

Then after some adaptations to the Portuguese language (I removed the option of 01/Feb/2015, only accepting months in numbers: 01/02/2015), share this powerful regular expression to validate dates:

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-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})$

The flow chart of the expression for better understanding:

Regular expression visualization

Online example in the tool used to debug and generate the Regular Expression flowchart (Debuggex)

0

check if date is in American format yyyy-mm-dd or Brazilian format dd/mm/yyyy, then validate as day 01 to 31, month 01 to 12 and year 1900 to 2999

/^((19[0-9][0-9]|2[0-9][0-9][0-9])-(0[1-9]|[1-9]|1[0-2]{1,2})-(0[1-9]|1[0-9]|2[0-9]|3[0-1]{1,2}))| ((0[1-9]|1[0-9]|2[0-9]|3[0-1]{1,2})/(0[1-9]|[1-9]|1[0-2]{1,2})/(19[0-9][0-9]|2[0-9][0-9][0-9]))$/g

Browser other questions tagged

You are not signed in. Login or sign up in order to post.