This is my reading of the spec:
#!/usr/bin/perl use strict; use warnings; use Regexp::Common; my $dur = qr /-? # Optional leading minus. P # Required. (?=[T\d]) # Duration cannot be empty. (?:(?!-) $RE{num}{int} Y)? # Non-negative integer, Y ( +optional) (?:(?!-) $RE{num}{int} M)? # Non-negative integer, M ( +optional) (?:(?!-) $RE{num}{int} D)? # Non-negative integer, D ( +optional) (?:T (?=\d) # T, must be followed by a +digit. (?:(?!-) $RE{num}{int} H)? # Non-negative integer, H ( +optional) (?:(?!-) $RE{num}{int} M)? # Non-negative integer, M ( +optional) (?:(?!-) $RE{num}{decimal} S)? # Non-negative decimal, S ( +optional) )? # Entire T part is optional /x; while (<DATA>) { chomp; print "$_ ", /^$dur$/ ? "matches\n" : "does not match\n"; } __DATA__ PnYnMnDTnHnMnS -P1Y P3Y3M3DT3H3M3S P3M3Y3DT3H3M3S P334Y3DT3H3M3S PT2H 5T PT P5M6DT P5M6DT9 P600T6S P1347Y P1347M P1Y2MT2H P0Y1347M P0Y1347M0D P-1347M -P1347M P1Y2MT P1Y2M P PT0S PT0.1234S

Running this results in:

PnYnMnDTnHnMnS does not match -P1Y matches P3Y3M3DT3H3M3S matches P3M3Y3DT3H3M3S does not match P334Y3DT3H3M3S matches PT2H matches 5T does not match PT does not match P5M6DT does not match P5M6DT9 does not match P600T6S does not match P1347Y matches P1347M matches P1Y2MT2H matches P0Y1347M matches P0Y1347M0D matches P-1347M does not match -P1347M matches P1Y2MT does not match P1Y2M matches P does not match PT0S matches PT0.1234S matches

Abigail


In reply to Re: duration regex by Abigail-II
in thread duration regex by Plankton

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.