Month + (space) +
Day + (space) +
(Optional:
Year + (space)
) +
Hour + (colon) +
Minute + (colon) +
Seconds +
(Optional:
(decimal point) + Fraction
)
####
use strict;
use warnings;
my $current_line = " Mar 11 08:02:08 172.28.17.253 Mar 11 2011 08:02:08 DR-FW-1 :";
$current_line .= "And also Apr 1 11:12:13 -- April's Fool is a fun day :)";
my @dates = $current_line =~ m/
( # Match and capture...
(?:Jan|Feb|Mar| # ... one of the twelve months
Apr|May|Jun| # I prefer to be explicit about this: it's
Jul|Aug|Sep| # a very limited set of strings that we accept
Oct|Nov|Dec # and [A-Z][a-z]{2} is too unrestrictive
)
\s+ # ... and one or more spaces
\d\d? # ... and one or two digits for the day, but note
# that this will also match for Feb 30,
# which doesn't exist, or for
# a day such as 54
\s+ # ... and one or more spaces
(?:\d{4}\s+)? # ... and optionally four digits for the year,
# followed by one or more spaces
\d\d:\d\d:\d\d # ... HH:MM:SS, but note that this will also
# acccept hours such as 34, or minutes such as
# 84, so this isn't the best we can do!
(?:\.\d*)? # ... and optionally a decimal point, which, if
# present, is optionally followed by fraction
# of second
) # End of capture.
/gxms;
print "I got ", scalar(@dates), " dates:\n";
print " => $_\n" for @dates;
print "But the first two are $dates[0] and $dates[1]\n";
####
I got 3 dates:
=> Mar 11 08:02:08
=> Mar 11 2011 08:02:08
=> Apr 1 11:12:13
But the first two are Mar 11 08:02:08 and Mar 11 2011 08:02:08