January 15, 2011
Date Validation using Regular Expression
Problem
Write a regular expression in Perl to validate dates in the following format “mm/dd/yyyy”. The month and day fields can be one or two digits.
Code
Here is a sample Perl code to do that
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/usr/bin/perl $date = " 1/1/2010 "; if ($date =~ /^\s*\d{1,2}/\d{1,2}/\d{4}\s*$/) { print "Format is correct"; } else { print "Format is not correct"; } |