There's a couple of ways to do this, using either a regex or (if your dates are always well-formed) 'split.' If you use a regex you need to do two things: 1) add a ^ character to anchor your regex to the beginning of your scalar. As it is your regex will match ANYWHERE in the string, not just the beginning. Using the ^ character you can restrict the match to the beginning of the string, which is what you want. Also, 2) you'll probably need to add capturing parentheses around the parts of the regex you're interested in. A matching regex without capturing parentheses only returns a "true" or "false" depending on whether a match is found or not. It does NOT return the match itself. (Well it does, but only if you use some funny variables... not recommended.) Like this:
#!/usr/bin/perl -w
use strict;
my $date= 'Sun Apr 1 10:27:03 CDT 2001';
if ($date =~ m/^(\w{3}\s+\w{3}\s+\d+)/) {
print "Matched $1\n";
}
Alternatively, you can use 'split' (this would be my choice.) Split will split the string up into space-divided chunks... so long as the order of the chunks doesn't change it's all good:
my $date= 'Sun Apr 1 10:27:03 CDT 2001';
my ($weekday, $month, $day) = split " ", $date;
print "$weekday $month $day\n";
Perlman perlfunc has more details on split if you're interested. Enjoy!
Gary Blackburn
Trained Killer