Nice regex V you can write this as well:
$line = "On January 1 1970 the unix epoch commenced."; my @months = qw( January Jan February Feb March Mar April Apr May June Jun July Jul August Aug September Sept Sep October Oct November Nov December Dec ); my $months = join "|", @months; my ($month, $day, $year) = $line =~ m/($months)\s+(\d+)\s+(\d+)/i; print "month\t$month\nday \t$day\nyear\t$year\n";
Logic is as explained by V. I have built up the month alternation from a list of months and abreviations stored in the array @months and joined with the | to allow interpolation into the regex . I have made the regex case insensitive with the /i switch. Note he used the /x switch to allow the informative comments in his regex. This also works and captures full or abbreviated months, case insensitive by allowing any sting of chars after the first theee which identify the month:
$line = "On January 1 1970 the unix epoch commenced."; my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my $months = join "|", @months; my ($month, $day, $year) = $line =~ m/((?:$months)\w+)\s+(\d+)\s+(\d+) +/i; print "month\t$month\nday \t$day\nyear\t$year\n";
cheers
tachyon
In reply to Re: Pulling Date out of String
by tachyon
in thread Pulling Date out of String
by GreatWhite
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |