syntax-check with regular expressions?

D

Detlef Jockheck

Hi,

I would like to check if a string contains a valid date format
(Format: "dd.mm.yyyy") or not.

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.

Sample code would help me very much.

regards
Detlef

PS: I'm a python newbie, so excuse this silly question
 
M

Matteo Dell'Amico

Detlef said:
Hi,

I would like to check if a string contains a valid date format
(Format: "dd.mm.yyyy") or not.

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.

Sample code would help me very much.

regards
Detlef

PS: I'm a python newbie, so excuse this silly question

This should work, returning True for valid dates and False otherwise:

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. :)
 
D

Duncan Booth

(e-mail address removed) (Detlef Jockheck) wrote in
Hi,

I would like to check if a string contains a valid date format
(Format: "dd.mm.yyyy") or not.

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.

Sample code would help me very much.

regards
Detlef

PS: I'm a python newbie, so excuse this silly question
Don't use a regexp for this sort of task, its much better to do the
conversion and catch an error if the conversion fails. In this case you
should use time.strptime with an appropriate format. Sample code as
requested:

Traceback (most recent call last):
File "<pyshell#8>", line 1, in -toplevel-
time.strptime("27.13.2004","%d.%m.%Y")
File "C:\Python23\lib\_strptime.py", line 424, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data=27.13.2004 fmt=%d.%m.%Y
Traceback (most recent call last):
File "<pyshell#15>", line 1, in -toplevel-
time.strptime("29.2.1800", "%d.%m.%Y")
File "C:\Python23\lib\_strptime.py", line 513, in strptime
julian = datetime_date(year, month, day).toordinal() - \
ValueError: day is out of range for month
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

Use time.strptime and it'll check everything for you.
 
A

Alexandre Fayolle

Le 27-07-2004 said:
Don't use a regexp for this sort of task, its much better to do the
conversion and catch an error if the conversion fails. In this case you
should use time.strptime with an appropriate format. Sample code as
requested:

Just as a side note, be careful with strptime, if you have cross
platform portability in mind. On older versions of python, this function
is not available on Windows, and on Unix systems, it relies on the C
library which is not always the GNU libc, which means that some
extensions available on linux systems are not available on Unix boxes.
We had the case on project which was deployed on AIX, and had to fight
with IBM's strptime implementation regarding white space handling. I
think it used to be picky with leading zeros too.
 
D

Detlef Jockheck

Matteo Dell'Amico said:
Detlef said:
Hi,

I would like to check if a string contains a valid date format
(Format: "dd.mm.yyyy") or not.

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.

Sample code would help me very much.

regards
Detlef

PS: I'm a python newbie, so excuse this silly question

This should work, returning True for valid dates and False otherwise:

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. :)

Hi!

That's great. Now I understand how this works. Thank you very much :)

regards
Detlef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top