CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
date validation

 



Anton
New User

Jul 19, 2002, 2:32 AM

Post #1 of 4 (3869 views)
date validation Can't Post

Hello,
I would to validate a date format (dd-mm-yyyy) with a regular expression. For the moment I use the following expression
regex="^[0-3 ][0-9 ]-[0-1 ][0-9 ]-[0-9 ][0-9 ][0-9 ][0-9 ]$"
but it doesn't recognize the bissextil year or 37-14-2002 is a valid date.
Thanks


davorg
Thaumaturge / Moderator

Jul 19, 2002, 6:45 AM

Post #2 of 4 (3864 views)
Re: [Anton] date validation [In reply to] Can't Post

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.

Code
#!/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


Anton
New User

Jul 19, 2002, 8:58 AM

Post #3 of 4 (3860 views)
Re: [davorg] date validation [In reply to] Can't Post

Thanks for the code but I really need to use a regex (I use a program which uses perl regex): validate a date with only one inline expression. Maybe I could combine more than one regex in one line?

regex="exp1 && exp2 && exp3 "

I don't know if it's possible?


davorg
Thaumaturge / Moderator

Jul 19, 2002, 9:15 AM

Post #4 of 4 (3856 views)
Re: [Anton] date validation [In reply to] Can't Post

But you're still trying to do somthing that's fundamentally not suited to regexes. I mean, let's look just at the the date. Currently you have:

Code
/[0-3][0-9]/

to match the data. But that actually matches any two digit number between 00 and 39. And some of those aren't valid dates. So let's try again:

Code
/([0-2][0-9])|(3[01])/

That's a bit better, it matches 00 to 31. But it still doesn't contain the intelligence to know which months have 30 or 31 days, so it still have the potential to give false positive matches. And I haven't even started to think about leap years.

You probably could do this in a regex. But it would be long and complex and almost completely unmaintainable.

In my opinion regular expressions are completely the wrong tool to use for this problem.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives