syntax-check with regular expressions?
Pierre-Frédéric Caillaud
peufeu at free.fr
Tue Jul 27 08:25:06 EDT 2004
Use time.strptime and it'll check everything for you.
> import re
>
> _re = re.compile(r'(\d{2})\.(\d{2})\.(\d{4})$')
> def testdate(s):
> try:
> day, month, year = [int(x) for x in _re.match(s).groups()]
> except AttributeError:
> return False
> return 1 <= day <= 31 and 1 <= month <= 12
>
> Implementing things such as different number of days for each month and
> leap years is left as an exercise to the reader. :-)
>
More information about the Python-list
mailing list