Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Sorry if my question is a little basic. I know some basic pattern matching, and have tried the tutorials section, but I'm still having a little trouble with the following task.
I have a load of date/time strings that have the following format:
mmm d{d} yyyy hh:mm:ss:sss{AM/PM}
eg. Oct 16 2004 11:09:19:943AM (example 1)
or Mar 3 2007 10:30:31:170PM (example 2)
What I want to do is to extract the day, month and year, and stick them together in the format yyyymmdd so I can sort by date easily. So for the above examples you'd get:
20041016 and
20070303
I've tried the following bit of code:
Now this works for example 2 (I do get 20070303), but not for example 1: for some reason I get 1016 instead of 20041016. Why?if ($data=~/(\D{3}) *?(\d{1,2}) *?(\d{4})/) # i.e. look fo +r 3 non-digits, then any number of spaces, then either 1 or two digit +s, then any number of spaces, then 4 digits. (I wasn't sure how many +spaces separated each part.) { $month= $datehash{$1},br; # datehash is a lookup for th +e month $day=$2; if ($day=~/\d{1}/) { $day='0'.$day; # i.e. add a '0' in front of days consistin +g of 1 digit, so 16 stays as 16, but 1 turns into 01 } $year=$3; $sortingdate=join '', $year, $month, $day; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pattern matching for extracting dates
by GrandFather (Saint) on Apr 16, 2007 at 12:42 UTC | |
by Anonymous Monk on Apr 16, 2007 at 13:06 UTC | |
|
Re: Pattern matching for extracting dates
by Krambambuli (Curate) on Apr 16, 2007 at 11:53 UTC | |
|
Re: Pattern matching for extracting dates
by Anno (Deacon) on Apr 16, 2007 at 12:05 UTC | |
|
Re: Pattern matching for extracting dates
by Herkum (Parson) on Apr 16, 2007 at 11:47 UTC | |
|
Re: Pattern matching for extracting dates
by johngg (Canon) on Apr 16, 2007 at 14:35 UTC | |
by Anonymous Monk on Apr 16, 2007 at 14:39 UTC |