Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm trying to write a script which will parse a line and try and extract some date information from it. The lines come from letters so, as far as I can tell, don't have a standard pattern. They can be:
"Thursday, 12th March 1843"
"12th March 1843"
"1843"
I'm trying to write a regex to find the months and if something like a month appears, turn it into a number and looking for some help in getting the month converted as I've gone down a wrong road and would like some directions back the right way.
# sub to print date sub get_date { my $text = shift or die "No date object passed through"; my %months = (JAN => '01', FEB => '02', MAR => '03', APR => '04', MAY=> '05', JUN => '06', JUL => '07', AUG => '08', SEP => '09', OCT => '10', NOV => '11', DEC => '12'); my $dttime = $text =~ m/((?<=_).*)/; $dttime = $1; my $month = $dttime =~ s/(\w{3}).*/$months{uc $1}/; $month = $1; my $year = $dttime =~ m/(\d{4})/; $year = $2; $dttime = $month . "-" . $year; return $dttime; }

Replies are listed 'Best First'.
Re: Trying to convert date from a line of text
by grep (Monsignor) on Jul 07, 2009 at 21:10 UTC
Re: Trying to convert date from a line of text
by moritz (Cardinal) on Jul 07, 2009 at 20:58 UTC
    Instead of re-inventing the wheel, you should take a look at existing wheels, for example Date::Parse - maybe some of them work for you out of the box, or with little effort?