Tuesday, February 21, 2023
HomeSoftware EngineeringRegex for Gregorian Date Validation in Python

Regex for Gregorian Date Validation in Python

The problem

Write a daily expression that validates the gregorian date within the format “DD.MM.YYYY”

Right date examples:


Incorrect examples:

" 01.08.1994"


  • most size of validator is 400 characters to keep away from hardcoding. (shortest answer to this point is 170 characters)
  • validator ought to course of leap days (February, 29) accurately.
  • the date is Gregorian, it’s vital to find out if yr is leap: https://en.wikipedia.org/wiki/Gregorian_calendar

The answer in Python code

Choice 1:

date_validator = (
    '(0[1-9]|1d|2[0-8]).(0[1-9]|1[012])|'    # 01-28 of any month
    '(29|30).(0[13-9]|1[012])|'               # 29-30 of months, besides February
    '(31.(0[13578]|1[02]))).'                # 31 of lengthy months
    '([1-9]d{3}|d{3}[1-9]))|'                # any yr, besides 0000
    '(29.02.('                               # leap day
    'dd([2468][048]|[13579][26]|0[48])|'     # leap years (mod 4)   
    '([2468][048]|[13579][26]|0[48])00'        # leap years (mod 400)
    ')))$' )

Choice 2:

not_0000  = "((?!0+$)d{4})"
not_feb   = "(0[13-9]|1[0-2])"
div_4     = "([2468][048]|[13579][26]|0[48])"
day_31    = "(31.(0[13578]|1[02]))"
day_29_30 = "(29|30)"
day_def   = "(0[1-9]|1d|2[0-8])"
two_int   = "d{2}"

day_29_30_31 = f"({day_31}|{day_29_30}.{not_feb}).{not_0000}"
day_default  = f"{day_def}.(02|{not_feb}).{not_0000}"
leap_year    = f"(29.02.({two_int}{div_4}|{div_4}00))"

date_validator = f"^({day_29_30_31}|{day_default}|{leap_year})$"

Choice 3:

date_validator = r"^(((0[1-9]|1d|2[0-8]).(0[1-9]|1[0-2])|(29|30).(0[13-9]|1[0-2])|31.(0[13578]|1[02])).(?!0000)d{4}$)|29.02.(?!0000)(([02468][048]|[13579][26])00|d{2}(0[48]|[2468][048]|[13579][26]))$"

Take a look at instances to validate our answer

import re
                    True, 'Primary appropriate date: 01.01.2009')
                    False, 'Incorrect masks: 01-Jan-2009')
                    False, 'Incorrect month: 15.15.2009')


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto