I am writing a regex to test for valid dates in YYYY-MM-DD format.
Using this regex in a perl test program I get positive results for all dates for the next 365 days.
Regex Coach however failes to match some such as '2010-06-30'. I assume this is because of the order of the alternations and the regex wanting to match a sub-pattern as quickly as possible. I can resolve the issue in The Regex Coach by adding () around the month and year patterns.
I would like to know why there is a difference between my Perl program and Regex Coach. Could this be implementation dependent. Both my program and its output are below.
I tend to think that The Regex Coach got this right.
(?# regex in The Regex Coach) * [123456789]\d\d\d- # year * * (?: * * (?:0[13578]|10|12 - [012][123456789]|10|20|30|31) | # Months with 31 days* * (?:0[469]|11 - [012][123456789]|10|20|30 ) | # Months with 30 days * * (?:02 - [012][12345678]||10|20|19 ) # Febuary* * )*
### perl Test program #!/iw-home/teamsite/iw-perl/bin/iwperl
use strict; use warnings; use Carp; use Getopt::Long; use Pod::Usage; use Cwd; use Date::Calc qw(:all);
my $dateregex = qr{ [123456789]\d\d\d- # year (?: (?:0[13578]|10|12 - [012][123456789]|10|20|30|31) | # Months with 31 days (?:0[469]|11 - [012][123456789]|10|20|30 ) | # Months with 30 days (?:02 - [012][12345678]||10|20|19 ) # Febuary ) }smx;
sub testdate { my ($d) = (@_); print "$d"; if ( $d =~ m{$dateregex}smx ) { print "okay\n"; } else { print "\n"; croak "broken"; } }
foreach my $d ( 1 .. 365 ) { my ( $year, $month, $day, $hour, $min, $sec, $doy, $dow, $dst ) = System_Clock(); ( $year, $month, $day, $hour, $min, $sec ) = Add_Delta_DHMS( $year, $month, $day, '0', '0', '0', "$d", '0', '0', '0' );
my $td = sprintf( "%4d-%02d-%02d", $year, $month, $day ); testdate("$td");
} exit;
##OUTPUT 2009-08-03okay 2009-08-04okay 2009-08-05okay 2009-08-06okay 2009-08-07okay 2009-08-08okay 2009-08-09okay 2009-08-10okay 2009-08-11okay 2009-08-12okay 2009-08-13okay 2009-08-14okay 2009-08-15okay 2009-08-16okay 2009-08-17okay 2009-08-18okay 2009-08-19okay 2009-08-20okay 2009-08-21okay 2009-08-22okay 2009-08-23okay 2009-08-24okay 2009-08-25okay 2009-08-26okay 2009-08-27okay 2009-08-28okay 2009-08-29okay 2009-08-30okay 2009-08-31okay 2009-09-01okay 2009-09-02okay 2009-09-03okay 2009-09-04okay 2009-09-05okay 2009-09-06okay 2009-09-07okay 2009-09-08okay 2009-09-09okay 2009-09-10okay 2009-09-11okay 2009-09-12okay 2009-09-13okay 2009-09-14okay 2009-09-15okay 2009-09-16okay 2009-09-17okay 2009-09-18okay 2009-09-19okay 2009-09-20okay 2009-09-21okay 2009-09-22okay 2009-09-23okay 2009-09-24okay 2009-09-25okay 2009-09-26okay 2009-09-27okay 2009-09-28okay 2009-09-29okay 2009-09-30okay 2009-10-01okay 2009-10-02okay 2009-10-03okay 2009-10-04okay 2009-10-05okay 2009-10-06okay 2009-10-07okay 2009-10-08okay 2009-10-09okay 2009-10-10okay 2009-10-11okay 2009-10-12okay 2009-10-13okay 2009-10-14okay 2009-10-15okay 2009-10-16okay 2009-10-17okay 2009-10-18okay 2009-10-19okay 2009-10-20okay 2009-10-21okay 2009-10-22okay 2009-10-23okay 2009-10-24okay 2009-10-25okay 2009-10-26okay 2009-10-27okay 2009-10-28okay 2009-10-29okay 2009-10-30okay 2009-10-31okay 2009-11-01okay 2009-11-02okay 2009-11-03okay 2009-11-04okay 2009-11-05okay 2009-11-06okay 2009-11-07okay 2009-11-08okay 2009-11-09okay 2009-11-10okay 2009-11-11okay 2009-11-12okay 2009-11-13okay 2009-11-14okay 2009-11-15okay 2009-11-16okay 2009-11-17okay 2009-11-18okay 2009-11-19okay 2009-11-20okay 2009-11-21okay 2009-11-22okay 2009-11-23okay 2009-11-24okay 2009-11-25okay 2009-11-26okay 2009-11-27okay 2009-11-28okay 2009-11-29okay 2009-11-30okay 2009-12-01okay 2009-12-02okay 2009-12-03okay 2009-12-04okay 2009-12-05okay 2009-12-06okay 2009-12-07okay 2009-12-08okay 2009-12-09okay 2009-12-10okay 2009-12-11okay 2009-12-12okay 2009-12-13okay 2009-12-14okay 2009-12-15okay 2009-12-16okay 2009-12-17okay 2009-12-18okay 2009-12-19okay 2009-12-20okay 2009-12-21okay 2009-12-22okay 2009-12-23okay 2009-12-24okay 2009-12-25okay 2009-12-26okay 2009-12-27okay 2009-12-28okay 2009-12-29okay 2009-12-30okay 2009-12-31okay 2010-01-01okay 2010-01-02okay 2010-01-03okay 2010-01-04okay 2010-01-05okay 2010-01-06okay 2010-01-07okay 2010-01-08okay 2010-01-09okay 2010-01-10okay 2010-01-11okay 2010-01-12okay 2010-01-13okay 2010-01-14okay 2010-01-15okay 2010-01-16okay 2010-01-17okay 2010-01-18okay 2010-01-19okay 2010-01-20okay 2010-01-21okay 2010-01-22okay 2010-01-23okay 2010-01-24okay 2010-01-25okay 2010-01-26okay 2010-01-27okay 2010-01-28okay 2010-01-29okay 2010-01-30okay 2010-01-31okay 2010-02-01okay 2010-02-02okay 2010-02-03okay 2010-02-04okay 2010-02-05okay 2010-02-06okay 2010-02-07okay 2010-02-08okay 2010-02-09okay 2010-02-10okay 2010-02-11okay 2010-02-12okay 2010-02-13okay 2010-02-14okay 2010-02-15okay 2010-02-16okay 2010-02-17okay 2010-02-18okay 2010-02-19okay 2010-02-20okay 2010-02-21okay 2010-02-22okay 2010-02-23okay 2010-02-24okay 2010-02-25okay 2010-02-26okay 2010-02-27okay 2010-02-28okay 2010-03-01okay 2010-03-02okay 2010-03-03okay 2010-03-04okay 2010-03-05okay 2010-03-06okay 2010-03-07okay 2010-03-08okay 2010-03-09okay 2010-03-10okay 2010-03-11okay 2010-03-12okay 2010-03-13okay 2010-03-14okay 2010-03-15okay 2010-03-16okay 2010-03-17okay 2010-03-18okay 2010-03-19okay 2010-03-20okay 2010-03-21okay 2010-03-22okay 2010-03-23okay 2010-03-24okay 2010-03-25okay 2010-03-26okay 2010-03-27okay 2010-03-28okay 2010-03-29okay 2010-03-30okay 2010-03-31okay 2010-04-01okay 2010-04-02okay 2010-04-03okay 2010-04-04okay 2010-04-05okay 2010-04-06okay 2010-04-07okay 2010-04-08okay 2010-04-09okay 2010-04-10okay 2010-04-11okay 2010-04-12okay 2010-04-13okay 2010-04-14okay 2010-04-15okay 2010-04-16okay 2010-04-17okay 2010-04-18okay 2010-04-19okay 2010-04-20okay 2010-04-21okay 2010-04-22okay 2010-04-23okay 2010-04-24okay 2010-04-25okay 2010-04-26okay 2010-04-27okay 2010-04-28okay 2010-04-29okay 2010-04-30okay 2010-05-01okay 2010-05-02okay 2010-05-03okay 2010-05-04okay 2010-05-05okay 2010-05-06okay 2010-05-07okay 2010-05-08okay 2010-05-09okay 2010-05-10okay 2010-05-11okay 2010-05-12okay 2010-05-13okay 2010-05-14okay 2010-05-15okay 2010-05-16okay 2010-05-17okay 2010-05-18okay 2010-05-19okay 2010-05-20okay 2010-05-21okay 2010-05-22okay 2010-05-23okay 2010-05-24okay 2010-05-25okay 2010-05-26okay 2010-05-27okay 2010-05-28okay 2010-05-29okay 2010-05-30okay 2010-05-31okay 2010-06-01okay 2010-06-02okay 2010-06-03okay 2010-06-04okay 2010-06-05okay 2010-06-06okay 2010-06-07okay 2010-06-08okay 2010-06-09okay 2010-06-10okay 2010-06-11okay 2010-06-12okay 2010-06-13okay 2010-06-14okay 2010-06-15okay 2010-06-16okay 2010-06-17okay 2010-06-18okay 2010-06-19okay 2010-06-20okay 2010-06-21okay 2010-06-22okay 2010-06-23okay 2010-06-24okay 2010-06-25okay 2010-06-26okay 2010-06-27okay 2010-06-28okay 2010-06-29okay 2010-06-30okay 2010-07-01okay 2010-07-02okay 2010-07-03okay 2010-07-04okay 2010-07-05okay 2010-07-06okay 2010-07-07okay 2010-07-08okay 2010-07-09okay 2010-07-10okay 2010-07-11okay 2010-07-12okay 2010-07-13okay 2010-07-14okay 2010-07-15okay 2010-07-16okay 2010-07-17okay 2010-07-18okay 2010-07-19okay 2010-07-20okay 2010-07-21okay 2010-07-22okay 2010-07-23okay 2010-07-24okay 2010-07-25okay 2010-07-26okay 2010-07-27okay 2010-07-28okay 2010-07-29okay 2010-07-30okay 2010-07-31okay 2010-08-01okay 2010-08-02okay