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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.