develooper Front page | perl.fwp | Postings from November 2011

date validation via regex

Thread Next
From:
Yitzchak Scott-Thoennes
Date:
November 15, 2011 11:01
Subject:
date validation via regex
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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About