Your regular expression is okay, as far as it goes. The error is in the formatting. The test $day=~/\d{1}/ will match every one- or two-digit day specification, so you will always add a zero. Also, do the values of %datehash have leading zeroes where required? Here is how I would re-write your code:if ($data=~/(\D{3}) *?(\d{1,2}) *?(\d{4})/) { $month = $datehash{$1}; $day=$2; if ($day=~/\d{1}/) { $day='0'.$day; } $year=$3; $sortingdate=join '', $year, $month, $day; }
I have made the regular expression more specific for the month names (since %datehash is already there) and less specific for white space (allowing any, not only blanks).my %datehash; @datehash{ qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) } = map sprintf( '%02d', $_), 1 .. 12; my $mon_re = join '|', keys %datehash; while ( <DATA> ) { my ( $mon, $day, $year) = /($mon_re)\s+(\d\d?)\s+(\d{4})/; $day = sprintf '%02d', $day; print "$year$datehash{ $mon}$day\n"; } __DATA__ Oct 16 2004 11:09:19:943AM Mar 3 2007 10:30:31:170PM
Anno
In reply to Re: Pattern matching for extracting dates
by Anno
in thread Pattern matching for extracting dates
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |