Does the following work by chance?

my ($date, $year) = /(\w+\s+\d+),?\s+(\d{4})/; print "$date, $year\n";

Explanation inside an example:

use warnings; use strict; while (<DATA>){ if (my ($date, $year) = / ( # start capture 1 ($date) \w+ # word characters (month) \s+ # space characters \d+ # date chars ) # end capture 1 ,? # comma or no comma, \s+ # followed by possible whitespace ( # start capture 2 ($year) \d{4} # four digits (year) ) # end capture 2 /x ) { print "$date, $year\n"; } } __DATA__ For the fiscal year ended Dec 31, 2015xxx For the fiscal year ending December 31 2015 For the fiscal year ending December 31, 2015

Output:

Dec 31, 2015 December 31, 2015 December 31, 2015

That regex may work, but it has the potential for significant failure rates. Unless you know your data very well, test the results extensively.


In reply to Re: REGEX for date by stevieb
in thread REGEX for date by wrkrbeee

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.