Syntax problem

Discussion in 'Perl Misc' started by Glenn J. Rowe, Dec 5, 2003.

  1. I need to read in a variable and make sure it is in the exact format of
    YYYY_MMM_DD_hh:mm:ss
    Example... 2003_DEC_05_14:05:26


    I tried the following code but it doesn't work...
    if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)
    {
    #Continue with code
    }
    else
    {
    #Display an error
    }

    I am a self taught programmer so any help you can give me would be
    appreciated.

    Thanks,

    Glenn
     
    Glenn J. Rowe, Dec 5, 2003
    #1
    1. Advertising

  2. Glenn J. Rowe

    Robert Dover Guest

    Glenn J. Rowe wrote:

    > I need to read in a variable and make sure it is in the exact format of
    > YYYY_MMM_DD_hh:mm:ss
    > Example... 2003_DEC_05_14:05:26
    >
    >
    > I tried the following code but it doesn't work...
    > if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)
    > {
    > #Continue with code
    > }
    > else
    > {
    > #Display an error
    > }


    I didn't rigorously test it, but it seems to work for me. What problems are you
    seeing?

    -Bob
     
    Robert Dover, Dec 5, 2003
    #2
    1. Advertising

  3. Glenn J. Rowe

    Robert Dover Guest

    Robert Dover wrote:

    > Glenn J. Rowe wrote:
    >
    > I didn't rigorously test it, but it seems to work for me. What problems
    > are you seeing?


    Sorry to follow-up on my own post, but here's what I did:

    #!/usr/bin/perl -w
    $variable = "2003_DEC_05_14:05:26";
    if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)
    {print "ok\n";}
    else {print "nope\n";}

    Some points:

    -Your regex will also match "abc 1234_SOP_13_99:99:99 something else".
    -If you're getting your $variable from STDIN, don't forget to chomp it.

    HTH,
    BD
     
    Robert Dover, Dec 5, 2003
    #3
  4. On Fri, 5 Dec 2003 14:16:09 -0500, Glenn J. Rowe wrote:
    > I need to read in a variable and make sure it is in the exact format
    > of YYYY_MMM_DD_hh:mm:ss
    > Example... 2003_DEC_05_14:05:26
    >
    > I tried the following code but it doesn't work...


    [aside: "doesn't work" is usually not the best choice of
    phrase to describe a programming problem...]

    > if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)


    Apart from removing the unnecessary parens, I would skip the {N}
    and add ^, $ anchors for clarity (assuming that doesn't cause
    other problems), and maybe tweak the "month" field:

    /^\d\d\d\d_\w\w\w_\d\d_\d\d:\d\d:\d\d$/ # untested

    Of course this doesn't verify that you have a valid date at all.

    But I doubt this would fix your "doesn't work" problem. Code like:

    warn "\$variable is '$variable'";
    if ($variable =~ /whatever/) { ... }

    can help determine exactly what text you're using in the match.
     
    John J. Trammell, Dec 5, 2003
    #4
  5. Glenn J. Rowe

    Ben Morrow Guest

    "Glenn J. Rowe" <undisclosed_sender@undisclosed_domain.com> wrote:
    > I need to read in a variable and make sure it is in the exact format of
    > YYYY_MMM_DD_hh:mm:ss
    > Example... 2003_DEC_05_14:05:26
    >
    > I tried the following code but it doesn't work...
    > if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)


    my $month = join "|", qw/JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC/;

    if( $variable =~ /^ (\d{4}) _ ($month) _ (\d{2}) _
    (\d{2}) : (\d{2}) : (\d{2}) $/xo and
    $1 > 1900 and $1 < 2500 and $3 > 0 and $3 < 32 and
    $4 >= 0 and $4 < 24 and $5 >= 0 and $5 < 60 and $6 >= 0 and $6 < 60 ) {

    #pass
    } else {
    #fail
    }

    Or I would use one of the date-parsing modules from CPAN.

    Ben

    --
    If I were a butterfly I'd live for a day, / I would be free, just blowing away.
    This cruel country has driven me down / Teased me and lied, teased me and lied.
    I've only sad stories to tell to this town: / My dreams have withered and died.
    <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=> (Kate Rusby)
     
    Ben Morrow, Dec 5, 2003
    #5
  6. Hmmm - I "just" noticed something...

    Up till now I was testing the code by changing the first part to either 203
    or 20003 instead of 2003 and the code didn't work.

    I just tried messing up other parts and it worked fine. (ie. NOV changed to
    NV)

    I changed the code to
    if($variable=~m/2(\d{3})_(\D{3})_(\d{2})_(\d{2}):(\d{2}):(\d{2})/) and all
    is fine.

    Not sure why it only works if I specify the first character but hey it
    works.

    Thanks for your help,

    Glenn



    "Robert Dover" <> wrote in message
    news:bqqohe$ns9$...
    > Robert Dover wrote:
    >
    > > Glenn J. Rowe wrote:
    > >
    > > I didn't rigorously test it, but it seems to work for me. What problems
    > > are you seeing?

    >
    > Sorry to follow-up on my own post, but here's what I did:
    >
    > #!/usr/bin/perl -w
    > $variable = "2003_DEC_05_14:05:26";
    >

    if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)
    > {print "ok\n";}
    > else {print "nope\n";}
    >
    > Some points:
    >
    > -Your regex will also match "abc 1234_SOP_13_99:99:99 something else".
    > -If you're getting your $variable from STDIN, don't forget to chomp it.
    >
    > HTH,
    > BD
    >
     
    Glenn J. Rowe, Dec 5, 2003
    #6
  7. Glenn J. Rowe <undisclosed_sender@undisclosed_domain.com> wrote:

    > Subject: Syntax problem



    You do not have a syntax problem, your code compiles just fine.

    It must be some other kind of problem.


    > I need to read in a variable and make sure it is in the exact format of
    > YYYY_MMM_DD_hh:mm:ss
    > Example... 2003_DEC_05_14:05:26
    >
    >
    > I tried the following code but it doesn't work...



    What does "doesn't work" mean when you say it?


    > if($variable=~m/(\d{4})(_)(\D{3})(_)(\d{2})(_)(\d{2}):))(\d{2}):))(\d{2})/)



    We need two things (a string and a pattern) in order to evaluate the
    behavior of a pattern match, but we only have one of them.

    What value is in $variable?

    Your pattern looks fine to me (apart from a bunch of parenthesis
    that are not needed).


    > I am a self taught programmer so any help you can give me would be
    > appreciated.



    Read the Posting Guidelines that are posted here frequently.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 6, 2003
    #7
  8. Glenn J. Rowe wrote:
    > I need to read in a variable and make sure it is in the exact format
    > of YYYY_MMM_DD_hh:mm:ss


    A namesake of yours just posted exactly the same question to alt.perl.
    You may want to check there for answers.

    jue
     
    Jürgen Exner, Dec 6, 2003
    #8
    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. gabriele renzi
    Replies:
    2
    Views:
    220
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    223
  3. Good Night Moon
    Replies:
    9
    Views:
    290
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    325
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    323
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page