For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > regexp to match time string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regexp to match time string
Brandon Metcalf

2004-05-20, 11:34 am

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.


--
Brandon
James Willmore

2004-05-20, 2:31 pm

On Thu, 20 May 2004 14:54:20 +0000, Brandon Metcalf wrote:

> 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.

Ben Morrow

2004-05-20, 2:31 pm


Quoth Brandon Metcalf <bmetcalf@nortelnetworks.com>:
> 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

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
Brandon Metcalf

2004-05-20, 4:33 pm

On 2004-05-20, Ben Morrow <usenet@morrow.me.uk> wrote:
>
> Quoth Brandon Metcalf <bmetcalf@nortelnetworks.com>:
>
> 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.


--
Brandon
Uri Guttman

2004-05-21, 12:31 am

>>>>> "JW" == James Willmore <jwillmore@remove.adelphia.net> writes:

JW> On Thu, 20 May 2004 14:54:20 +0000, Brandon Metcalf wrote:[color=darkred]

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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
James Willmore

2004-05-21, 1:31 am

On Fri, 21 May 2004 03:23:10 +0000, Uri Guttman wrote:

>
> JW> On Thu, 20 May 2004 14:54:20 +0000, Brandon Metcalf wrote:
>
> 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


Charles DeRykus

2004-05-21, 5:31 am

In article <slrncapho7.cum.bmetcalf@cash.rhiamet.com>,
Brandon Metcalf <bmetcalf@nortelnetworks.com> wrote:
>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.

--
Charles DeRykus





Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com