date validation via regex

Yitzchak Scott-Thoennes <[email protected]> Tue, 15 Nov 2011 11:01:48 -0800
Newsgroups gmane.comp.lang.perl.fun
Message-ID <CAN7g7HVKFGPRgc9H6Q8GK+nvLZUiYOHEyz6=waUpRLS+zNa+=w@mail.gmail.com>
This is something people often ask for.  The stock answer is that
regexes aren't the correct solution, but really it's not so hard.

This validates Gregorian MM/DD/CCYY dates, for example:

qr#^
    (?: 0[1-9] | 1[012] )
    /
    (?:
        0[1-9] | 1[0-9] | 2[0-8]
        | (?<! 0[2469]/ | 11/ ) 31
        | (?<! 02/ ) 30
        | (?<! 02/
             (?= ...
                 (?:
                     .. (?: [02468][1235679] | [13579][01345789] )
                     | (?: [02468][1235679] | [13579][01345789] ) 00
                 )
             )
        ) 29
    )
    /
    [0-9]{4}
    \z
#x