Nice regex V you can write this as well:

$line = "On January 1 1970 the unix epoch commenced."; my @months = qw( January Jan February Feb March Mar April Apr May June Jun July Jul August Aug September Sept Sep October Oct November Nov December Dec ); my $months = join "|", @months; my ($month, $day, $year) = $line =~ m/($months)\s+(\d+)\s+(\d+)/i; print "month\t$month\nday \t$day\nyear\t$year\n";

Logic is as explained by V. I have built up the month alternation from a list of months and abreviations stored in the array @months and joined with the | to allow interpolation into the regex . I have made the regex case insensitive with the /i switch. Note he used the /x switch to allow the informative comments in his regex. This also works and captures full or abbreviated months, case insensitive by allowing any sting of chars after the first theee which identify the month:

$line = "On January 1 1970 the unix epoch commenced."; my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my $months = join "|", @months; my ($month, $day, $year) = $line =~ m/((?:$months)\w+)\s+(\d+)\s+(\d+) +/i; print "month\t$month\nday \t$day\nyear\t$year\n";

cheers

tachyon


In reply to Re: Pulling Date out of String by tachyon
in thread Pulling Date out of String by GreatWhite

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.