Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Capturing Date/Time

by Anonymous Monk
on Aug 16, 2007 at 20:03 UTC ( [id://633163]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have the following string Aug 20, 2007 12:42 PM that may also optionally just be Aug 20, 2007. I'm trying to test/match/caputure both conditions in one expression. Is that possible?

This code works when the timestamp is present but obviously fails when the time is missing.

my $string = 'Aug 20, 2007 12:42 PM'; if ($string =~ /(\w{3}) (\d+), (\d{4}) (\d+):(\d{2}) (.+)/) { # do stuff }

How do I make the time portion optional in that expression? This doesn't work:

if ($string =~ /(\w{3}) (\d+), (\d{4}) ((\d+):(\d{2}) (.+)?)/) { # do stuff }

Thanks.

Replies are listed 'Best First'.
Re: Capturing Date/Time
by Zaxo (Archbishop) on Aug 16, 2007 at 21:34 UTC

    You might like Date::Parse for that. It's a lot more flexible about the datestring formats it will handle.

    After Compline,
    Zaxo

Re: Capturing Date/Time
by FunkyMonk (Chancellor) on Aug 16, 2007 at 20:16 UTC
    You were nearly there...
    /(\w{3}) (\d+), (\d{4})(?: (\d+):(\d\d) )?/

    (?: ... ) are non-capturing parentheses, so they don't muck up your dollar-digit captures.

    Have you considered using split instead of a regexp? In this case it leads to much easier code to maintain

    my @date_parts = split m{[\s:,]+}, $date_string; if ( @date_parts == 6 ) { # date + time } elsif ( @date_parts == 3 ) { # date only } else { die "invalid date/time combo"; }

    update: added split alternative

      I like FunkyMonk's solution, but here's another:
      if ($time =~ /^\w{3}\s\d{2}\,\s\d{4}(\s\d{2}:\d{2}\s\w{2})?/){ print "Found it\n"; }
      I'm new to regex too, but I thought I'd give a crack at it too.
      Cheers!
      s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

      Don't personally like the split alternative as much. Neither approach here really do much to verify the data, but the regex is at least a bit more strict. So the regex fails to match for a string like "foo bar, baz" but the split passes it along as a valid date.

      Obviously if verification of valid dates is required to prevent something like "Feb 99, 0000" for instance, then a module of some sort would probably be the best approach. (Although I can't suggest a particular module off the top of my head)

        Validation of the data was not one of my goals, as per the OP. If it had been, I'd have used one of the (far too1) many date modules available.
        1 A personal beef of mine. Don't worry about it.

Re: Capturing Date/Time
by akho (Hermit) on Aug 17, 2007 at 08:56 UTC
    You may also like Date::Manip. It will parse a date in almost any format you throw at it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://633163]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found