regexp to match time string

Discussion in 'Perl Misc' started by Brandon Metcalf, May 20, 2004.

  1. 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
     
    Brandon Metcalf, May 20, 2004
    #1
    1. Advertising

  2. 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.
     
    James Willmore, May 20, 2004
    #2
    1. Advertising

  3. Brandon Metcalf

    Ben Morrow Guest

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


    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.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, May 20, 2004
    #3
  4. On 2004-05-20, Ben Morrow <> wrote:
    >
    > Quoth 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?

    >
    > 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
     
    Brandon Metcalf, May 20, 2004
    #4
  5. Brandon Metcalf

    Uri Guttman Guest

    >>>>> "JW" == James Willmore <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 21, 2004
    #5
  6. On Fri, 21 May 2004 03:23:10 +0000, Uri Guttman wrote:

    >>>>>> "JW" == James Willmore <> 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
     
    James Willmore, May 21, 2004
    #6
  7. In article <>,
    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})?$/
    >


    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
     
    Charles DeRykus, May 21, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    19,692
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    487
    Mikel Lindsaar
    Mar 31, 2008
  3. Old Echo
    Replies:
    1
    Views:
    181
    Adam Shelly
    Sep 4, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    361
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    191
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page