in reply to Generating a format template for a date

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