Use the (??{}) construct and it's pretty easy:

#!/usr/local/bin/perl use warnings; use strict; use Date::Simple; $|++; my $good_date = sub { my ($year,$month,$day) = ($1, $2, $3); my $date = do { no warnings 'uninitialized'; Date::Simple->new($year,$month,$day) }; return $date ? qr{(?=)} : qr{(?!)}; }; # CCYY-MM-DD[tz] my $pat = qr /^(\d\d\d\d)-(\d\d)-(\d\d)(??{$good_date->()})/; while (<DATA>) { chomp; print "$_ ", /$pat/ ? "matches\n" : "does not match\n"; } __DATA__ 1968-04-02 -0045-01-01 11968-04-02 1968-04-02+05:00 1968-04-02Z invalids to follow 68-04-02 1968-4-2 1968/04/02 04-02-1968 1968-04-31

And the output:

1968-04-02 matches
-0045-01-01 does not match
11968-04-02 does not match
1968-04-02+05:00 matches
1968-04-02Z matches
invalids to follow does not match
68-04-02 does not match
1968-4-2 does not match
1968/04/02 does not match
04-02-1968 does not match
1968-04-31 does not match

I gave a simplified example to show you how it's done, but you'll want to flesh out that regex more.

Cheers,
Ovid

New address of my CGI Course.


In reply to Re: date regex Can this be done? by Ovid
in thread date regex by Plankton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.