http://www.perlmonks.org?node_id=1054788


in reply to Formatting STDIN for date format

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:

$ pm_parse_stdin.pl 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:

$ pm_parse_stdin_2.pl 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