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 | |
|
Re: Capturing Date/Time
by FunkyMonk (Bishop) on Aug 16, 2007 at 20:16 UTC | |
by logie17 (Friar) on Aug 16, 2007 at 21:29 UTC | |
by Nkuvu (Priest) on Aug 16, 2007 at 21:08 UTC | |
by FunkyMonk (Bishop) on Aug 16, 2007 at 23:35 UTC | |
|
Re: Capturing Date/Time
by akho (Hermit) on Aug 17, 2007 at 08:56 UTC |