This attempt uses regular expression code blocks to set the format string. I was hoping to find a way to use regex conditional patterns but couldn't get it to work. Here it is

use strict; use warnings; my $fmt; print map { sprintf qq{Date: %-12sFormat: %-s\n}, $_->[0], $_->[1] } map { $fmt = q{Date not valid}; m{(?x) ^\d\d(\D)\d\d(\D)\d{4}$ (?{$fmt = qq{DD${1}MM${2}YYYY}}) }; m{(?x) ^\d{4}(\D)\d\d(\D)\d\d$ (?{$fmt = qq{YYYY${1}MM${2}DD}}) }; m{(?x) ^\d\d(\D)\d\d(\D)\d\d$ (?{$fmt = qq{DD${1}MM${2}RR}}) }; [$_, $fmt]; } map {chomp; $_;} <DATA>; __END__ 99/99/9999 99/99/99 99-99-9999 99-99-99 99-99 99-99-999 99:99:9999 99:99:99 99+99+9999 9999-99-99 9999:99:99 9/99/99 9999+99+99 99-99.9999 99.99-9999

and the output is

Date: 99/99/9999 Format: DD/MM/YYYY Date: 99/99/99 Format: DD/MM/RR Date: 99-99-9999 Format: DD-MM-YYYY Date: 99-99-99 Format: DD-MM-RR Date: 99-99 Format: Date not valid Date: 99-99-999 Format: Date not valid Date: 99:99:9999 Format: DD:MM:YYYY Date: 99:99:99 Format: DD:MM:RR Date: 99+99+9999 Format: DD+MM+YYYY Date: 9999-99-99 Format: YYYY-MM-DD Date: 9999:99:99 Format: YYYY:MM:DD Date: 9/99/99 Format: Date not valid Date: 9999+99+99 Format: YYYY+MM+DD Date: 99-99.9999 Format: DD-MM.YYYY Date: 99.99-9999 Format: DD.MM-YYYY

I hope this is of interest.

Cheers,

JohnGG

Update: Got the regex conditionals working so here's the script the way I originally intended. It produces identical output to the first version.

Update 2: Broke the closing brackets of the conditionals onto separate lines with appropriate indentation to aid clarity.

use strict; use warnings; my $fmt; print map { sprintf qq{Date: %-12sFormat: %-s\n}, $_->[0], $_->[1] } map { m {(?x) ^ (?(?=\d\d(\D)\d\d(\D)\d{4}$) (?{$fmt = qq{DD${1}MM${2}YYYY}}) | (?(?=\d{4}(\D)\d\d(\D)\d\d$) (?{$fmt = qq{YYYY${3}MM${4}DD}}) | (?(?=\d\d(\D)\d\d(\D)\d\d$) (?{$fmt = qq{DD${5}MM${6}RR}}) | (?{$fmt = q{Date not valid}}) ) ) ) }; [$_, $fmt]; } map {chomp; $_;} <DATA>; __END__ 99/99/9999 99/99/99 99-99-9999 99-99-99 99-99 99-99-999 99:99:9999 99:99:99 99+99+9999 9999-99-99 9999:99:99 9/99/99 9999+99+99 99-99.9999 99.99-9999

In reply to Re: Generating a format template for a date by johngg
in thread Generating a format template for a date by bart

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.