#! perl -slw use strict; my $re_dur_years = qr[ (?<= P ) \d+ Y ]x; my $re_dur_months = qr[ (?<= [PY] ) \d+ M ]x; my $re_dur_days = qr[ (?<= [PYM] ) \d+ D ]x; my $re_dur_hours = qr[ (?<= [T] ) \d+ H ]x; my $re_dur_minutes = qr[ (?<= [TH] ) \d+ M ]x; my $re_dur_seconds = qr[ (?<= [THM] ) \d+ (?: \. \d+ )? S ]x; my $re_dur = qr[^ -? P (?= T? -? \d+ ) (?: $re_dur_years? | $re_dur_months? | $re_dur_days? (?: T (?: | $re_dur_hours | $re_dur_minutes | $re_dur_seconds ){1,3} )? ){1,6} $]x; while( ) { chomp; print and next if m[^\s*$]; printf "%s %s\n", $_, $_=~ $re_dur ? 'matched' : 'didn\'t match'; } __DATA__ P P1Y P1Y1M P1Y1M1D P1Y1M1DT1H P1Y1M1DT1H1M P1Y1M1DT1H1M1S P1Y1M1DT1H1M1.1S P1M1DT1H1M1.1S P1DT1H1M1.1S PT1H1M1.1S PT1M1.1S PT1.1S PT0.1S P- P-1Y P-1Y-1M P-1Y-1M-1D P-1Y-1M-1DT-1H P-1Y-1M-1DT-1H-1M P-1Y-1M-1DT-1H-1M-1S P-1Y-1M-1DT-1H-1M-1.1S P-1M-1DT-1H-1M-1.1S P-1DT-1H-1M-1.1S PT-1H-1M-1.1S PT-1M-1.1S PT-1.1S PT-0.1S P1H P1M1Y P1D1M P1DT1M PT1Y PT1M PT1D PT1S1M PT1M1H -P1Y P3Y3M3DT3H3M3S P3M3Y3DT3H3M3S P334Y3DT3H3M3S PT2H 5T PT P5M6DT P5M6DT9 P600T6S