while ( <DATA> ) { s!\b((\d{4}[-/]\d{2}[-/]\d{2}\b)|(\d{2}[-/]\d{2}[-/]\d{4}))\b!<date +>$1</date>!g; print; } __DATA__ 2000-04-23 was a great day, but 04-24-1999 is much better. 2000/04/23 I would like to match those 3 formats for now. 2000-85-85 is not a real date.

The above regex does what you ask, but as noted by fellow monks, if you do not use a module, you will need to add other things to make sure what it is that you are matching is trully a date. As the above regex will accept 2000-73-08 which is not a valid date. One could write a regex to eliminate this possiblity, but as FamousLongAgo mentions there are vast subtleties as to what might be a valid date. (leap years, as one example,present a special problem). But then again only you know the data you are trying to match against.

As previously mentioned by saouq, if you give a better explanation of why don't want to use a module and what else you might need, someone will lead you in the right direction.


In reply to Re: Re: Re: matching different date formats by Enlil
in thread matching different date formats by imlou

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.