
davorg
Thaumaturge
/ Moderator
Jul 19, 2002, 6:45 AM
Post #2 of 4
(9869 views)
|
Regular expressions are probably the wrong way to attack this problem. Why not use Perl's date-handling capabilities to do it. Try and convert the values to a date using timelocal and if it fails, you know it wasn't a valid date.
#!/usr/bin/perl -w use Time::Local; while (<DATA>) { chomp; my ($d, $m, $y); unless (($d, $m, $y) = /(\d\d)-(\d\d)-(\d\d\d\d)/) { warn "$_ is not a valid date\n"; next; } --$m; $y -= 1900; eval "timelocal 0, 0, 0, $d, $m, $y"; warn "$_ is not a valid date - $@" if $@; } __END__ 07-09-2002 29-02-2002 01-10-01 Note that timelocal throws a fatal error if you pass it an invalid date, so we capture that with "eval". -- Dave Cross, Perl Hacker, Trainer and Writer http://www.dave.org.uk/ Get more help at Perl Monks
|