Other Monks have given you advice to solve your question but I note that you say

I have a load of date/time strings that have the following format

Your approach using yyyymmdd as the sorting string may not be adequate if more than one of those date/time strings falls on the same day. If this is likely to be a problem you should consider transforming the date/time into an epoch time value using the timelocal() subroutine in the Time::Local module. This can then be sorted numerically.

use strict; use warnings; use Time::Local; my %monthNumber = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11, ); my $rxDate = qr {(?x) ^ (\D{3}) \s+ (\d\d?) \s+ (\d{4}) \s+ (\d\d): (\d\d): (\d\d): (\d{3}) ([AP]M) }; my @dateStrs = ( q{Oct 16 2004 11:09:19:943AM}, q{Mar 3 2007 10:30:31:170PM}, q{Oct 16 2004 09:27:24:012PM}, q{Jun 4 2005 03:13:45:508AM}, ); print map { qq{$_->[0]\n} } sort { $a->[1] <=> $b->[1] } map { my $hr = $_->[4]; $hr += 12 if $_->[8] =~ m{^P}; my $timeVal = timelocal( $_->[6], $_->[5], $hr, $_->[2], $monthNumber{$_->[1]}, $_->[3] - 1900) + $_->[7] / 1000; [ $_->[0], $timeVal ] } grep { defined $_->[1] } map { [ $_, m{$rxDate} ] } @dateStrs;

and the output is

Oct 16 2004 11:09:19:943AM Oct 16 2004 09:27:24:012PM Jun 4 2005 03:13:45:508AM Mar 3 2007 10:30:31:170PM

I hope this is of use.

Cheers,

JohnGG


In reply to Re: Pattern matching for extracting dates by johngg
in thread Pattern matching for extracting dates by Anonymous Monk

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.