regexp to match time string

B

Brandon Metcalf

I'm trying to come up with a regexp to match a string containing the
date and time in the format:

yyyy-mm-dd hh:mm:ss

with either the date or time part being optional but not both and the
":ss" part always being optional. I have the following which falls short
in a number of ways:

/^(\d{4}(-\d{1,2}){1,2}\s+)?(\d{1,2}:)\d{2}){1,2})?$/

Is there a way to do this with one regexp?

Thanks.
 
J

James Willmore

I'm trying to come up with a regexp to match a string containing the
date and time in the format:

yyyy-mm-dd hh:mm:ss

with either the date or time part being optional but not both and the
":ss" part always being optional. I have the following which falls short
in a number of ways:

/^(\d{4}(-\d{1,2}){1,2}\s+)?(\d{1,2}:)\d{2}){1,2})?$/

Is there a way to do this with one regexp?

Thanks.

Not sure (and could be very wrong)

my @datetime = split /[-:]/, $the_string_to_parse;

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
If mathematically you end up with the wrong answer, try
multiplying by the page number.
 
B

Ben Morrow

Quoth Brandon Metcalf said:
I'm trying to come up with a regexp to match a string containing the
date and time in the format:

yyyy-mm-dd hh:mm:ss

with either the date or time part being optional but not both and the
":ss" part always being optional. I have the following which falls short
in a number of ways:

/^(\d{4}(-\d{1,2}){1,2}\s+)?(\d{1,2}:)\d{2}){1,2})?$/

Is there a way to do this with one regexp?

Ummm...

my $date = qr/\d{4}-\d{2}-\d{2}/;
my $time = qr/\d{2}:\d{2} (?: :\d{2} )?/x;
my $re = qr/^ (?: $date | $time | $date \s+ $time ) $/x;

Ben
 
B

Brandon Metcalf

Ummm...

my $date = qr/\d{4}-\d{2}-\d{2}/;
my $time = qr/\d{2}:\d{2} (?: :\d{2} )?/x;
my $re = qr/^ (?: $date | $time | $date \s+ $time ) $/x;

I think that'll do the trick. Thanks for the help.
 
U

Uri Guttman

JW> Not sure (and could be very wrong)

JW> my @datetime = split /[-:]/, $the_string_to_parse;

then how can you determine which are the missing parts? so i bet on very
wrong. :)

uri
 
J

James Willmore

JW> Not sure (and could be very wrong)

JW> my @datetime = split /[-:]/, $the_string_to_parse;

then how can you determine which are the missing parts? so i bet on very
wrong. :)

How about ...
my @datetime = split /[-: ]/, $the_string_to_parse; #I didn't consider the
space in the previous example.

Of course, this works if the line has only the pattern yyyy-mm-dd hh:mm:ss
-or- yyyy-mm-dd hh:mm and variations therein. And doesn't fully meet the
requirements the OP set forth :-( And, you can't be sure about the
missing parts :-(

But ... it's nice to post something like this in the hopes that someone
*may* be able to use it later on - so I don't feel all that bad about
missing the mark :)

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Philosophy will clip an angel's wings. -- John Keats
 
C

Charles DeRykus

I'm trying to come up with a regexp to match a string containing the
date and time in the format:

yyyy-mm-dd hh:mm:ss

with either the date or time part being optional but not both and the
":ss" part always being optional. I have the following which falls short
in a number of ways:

/^(\d{4}(-\d{1,2}){1,2}\s+)?(\d{1,2}:)\d{2}){1,2})?$/

Date::Manip could handle that too:

$date = Date::Manip::parseDate($string);
if ($date) { ... }
else { } # didn't match


Of course, Date::Manip would validate other representations like
"today", "1st thursday in June 1992", "8:00pm december tenth",
"05/10/93", etc. too in case that would stop the presses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top