Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regexp to match time string

Reply
Thread Tools

regexp to match time string

 
 
Brandon Metcalf
Guest
Posts: n/a
 
      05-20-2004
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
 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      05-20-2004
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.

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      05-20-2004

Quoth Brandon Metcalf <(E-Mail Removed)>:
> 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.
~ http://www.velocityreviews.com/forums/(E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
Brandon Metcalf
Guest
Posts: n/a
 
      05-20-2004
On 2004-05-20, Ben Morrow <(E-Mail Removed)> wrote:
>
> Quoth Brandon Metcalf <(E-Mail Removed)>:
>> 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;


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


--
Brandon
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-21-2004
>>>>> "JW" == James Willmore <(E-Mail Removed)> writes:

JW> 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})?$/


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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      05-21-2004
On Fri, 21 May 2004 03:23:10 +0000, Uri Guttman wrote:

>>>>>> "JW" == James Willmore <(E-Mail Removed)> writes:

>
> JW> 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})?$/

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


 
Reply With Quote
 
Charles DeRykus
Guest
Posts: n/a
 
      05-21-2004
In article <(E-Mail Removed)>,
Brandon Metcalf <(E-Mail Removed)> 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:arseDate($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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM
Regexp: how to match string that do not contain a word sfeher@gmail.com Java 3 07-21-2006 08:01 PM



Advertisments