Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day Ashley,

Welcome to the monastery.

You were sort of on the right track with capturing the elements of the date with "(\d{n})" constructs, but you appear to have thought that including variables within the regex would somehow assign to them. For starters, I'd recommend you look at "perlintro -- a brief introduction and overview of Perl" and "perlretut - Perl regular expressions tutorial".

If all you want to do is check the format and, if OK, build $sql, you can code this:

#!/usr/bin/env perl -l use strict; use warnings; while (<STDIN>) { chomp; if (/^(\d{2})-(\d{2})-(\d{4})$/) { print "... day='$1' month='$2' year='$3' ..."; } else { print 'Not DD-MM-YYYY format'; } }

Here's a sample run:

$ 19-9-2013 Not DD-MM-YYYY format 19/9/2013 Not DD-MM-YYYY format 30-02-2013 ... day='30' month='02' year='2013' ... 99-99-9999 ... day='99' month='99' year='9999' ... 19-09-2013 ... day='19' month='09' year='2013' ... 19-09-20133 Not DD-MM-YYYY format

That's correctly weeding out "19-9-2013" and "19/9/2013"; unfortunately, it's quite happy with possible typos like "30-02-2013" and completely bogus dates like "99-99-9999". So, you'll probably need to assign the captured values to variables and then validate them. There's lots of ways of doing this, and it may well depend on what sort of feedback you want to present to your users; here's a fairly straightforward method, using the builtin module Time::Piece, that differentiates between bad format, possible typo or leap year error, and completely nonsense dates:

#!/usr/bin/env perl -l use strict; use warnings; use Time::Piece; while (<STDIN>) { chomp; if (/^(\d{2})-(\d{2})-(\d{4})$/) { my ($day, $month, $year) = ($1, $2, $3); my $tp = eval { Time::Piece->strptime("$day$month$year", '%d%m +%Y') }; if (defined $tp) { if ($tp->ymd eq "$year-$month-$day") { print "... day='$day' month='$month' year='$year' ..." +; } else { print "INVALID DATE: $day-$month-$year"; } } else { print "BOGUS DATE: $day-$month-$year"; } } else { print 'Not DD-MM-YYYY format'; } }

Sample run:

$ 19-9-2013 Not DD-MM-YYYY format 19/09/2013 Not DD-MM-YYYY format 19-09-2013 ... day='19' month='09' year='2013' ... 99-99-9999 BOGUS DATE: 99-99-9999 32-02-2013 BOGUS DATE: 32-02-2013 31-02-2013 INVALID DATE: 31-02-2013 29-02-2013 INVALID DATE: 29-02-2013 29-02-2012 ... day='29' month='02' year='2012' ... 19-09-20133 Not DD-MM-YYYY format

-- Ken

In reply to Re: Formatting STDIN for date format by kcott
in thread Formatting STDIN for date format by Ashley Jordan

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2017-09-22 15:40 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (264 votes). Check out past polls.