perl regex to python

Remco Gerlich scarblac at pino.selwerd.nl
Wed Feb 7 12:54:19 EST 2001


nlymbo at my-deja.com <nlymbo at my-deja.com> wrote in comp.lang.python:
> Hi all, in perl i would do this to pick apart a date string:
> 
> if ($date_str =~ m#(\d+)/(\d+)/(\d+)#) {
>   print "month: $1, $day: $2, $year: $3\n";
> }
> 
> How does one save the matches in a reg expression using Python??
> Thanks.

If you're on Linux (or some other system that has strptime(), ie not Windows),
it'd be much easier to parse dates with t=time.strptime(s,"%m/%d/%Y"), and
similar.

>>> import time
>>> time.asctime(time.strptime("11/05/2001","%m/%d/%Y"))
'Mon Nov  5 00:00:00 2001'

If you need this on Windows, there are pure Python implementations out
there, somewhere.

-- 
Remco Gerlich



More information about the Python-list mailing list