syntax-check with regular expressions?

Michael Ekstrand python at
Tue Jul 27 14:23:21 CEST 2004

On Tuesday 27 July 2004 07:00, Detlef Jockheck wrote:
> What is the best way to do this. regexp? At the moment it would be
> sufficient to check for
> [digit][digit].[digit][digit].[digit][digit][digit][digit] but a full
> date verification (Month in range 1-12 ...) would be very nice too.

There is a way to do it with a regexp, I believe, but it is far from 
simple. Jeffery Friedl's book Mastering Regular Expressions (at least 
the first edition) develops such an expression as one of its examples.

The easy way, though, is the calendar module. You can do

import calendar
day, month, year = date.split('.')
if month < 1 or month > 12:
    raise ValueError("Invalid month")
if day < 1 or day > calendar.monthrange(year, month)[1]:
    raise ValueError("Invalid day")


More information about the Python-list mailing list