match leading '*' in r.e... how?

Discussion in 'Perl' started by Leor Zolman, Sep 27, 2003.

  1. Leor Zolman

    Leor Zolman Guest

    I'm probably going to feel really stupid when I see the answer, but I'm now
    stuck nevertheless... I need to match a leading literal '*', and Perl isn't
    getting the idea. Reading from standard input, typing "const" into the program
    below yields "just const" as expected, but typing "*const" ALSO results in it
    just saying "just const", rather than "*const".

    This is a simplification of a much more complex r.e. where I need to detect an
    asterisk immediately preceding "const", and it isn't working either.

    How am I being brain-dead?
    -leor

    while (<>)
    {
    if (/const/)
    {
    print "just const\n\n";
    }
    elsif (/\*const/)
    {
    print "*const\n\n";
    }
    else
    {
    print "None.\n";
    }
    }
     
    Leor Zolman, Sep 27, 2003
    #1
    1. Advertisements

  2. Leor Zolman

    John Bokma Guest

    Leor Zolman wrote:

    > I'm probably going to feel really stupid when I see the answer, but I'm now
    > stuck nevertheless... I need to match a leading literal '*', and Perl isn't
    > getting the idea. Reading from standard input, typing "const" into the program
    > below yields "just const" as expected, but typing "*const" ALSO results in it
    > just saying "just const", rather than "*const".
    >
    > This is a simplification of a much more complex r.e. where I need to detect an
    > asterisk immediately preceding "const", and it isn't working either.
    >
    > How am I being brain-dead?
    > -leor
    >
    > while (<>)
    > {
    > if (/const/)
    > {
    > print "just const\n\n";


    Nope, /const/ matches *const, constant, fooconstbar etc.

    Always print the thingy when debugging... Not a silly message which
    assumes your program works (which it doesn't).

    > }
    > elsif (/\*const/)
    > {
    > print "*const\n\n";
    > }
    > else
    > {
    > print "None.\n";
    > }
    > }


    To fix it: match the most specific one first. Also read how one can
    create a "switch" in perl. It will quite likely make your code more
    readable.


    --
    Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
    web site hints: http://johnbokma.com/websitedesign/
    John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen
     
    John Bokma, Sep 27, 2003
    #2
    1. Advertisements

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. hiwa
    Replies:
    0
    Views:
    807
  2. Victor
    Replies:
    2
    Views:
    806
    Victor
    May 17, 2004
  3. John Nagle
    Replies:
    4
    Views:
    798
    James Antill
    Aug 7, 2007
  4. ekzept
    Replies:
    0
    Views:
    565
    ekzept
    Aug 10, 2007
  5. John Gordon
    Replies:
    13
    Views:
    741
    Ian Kelly
    Dec 20, 2011
  6. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    301
  7. Old Echo
    Replies:
    1
    Views:
    305
    Adam Shelly
    Sep 4, 2008
  8. Leor Zolman

    how to match leading '*' ??

    Leor Zolman, Sep 27, 2003, in forum: Perl Misc
    Replies:
    7
    Views:
    242
    Bob Walton
    Sep 27, 2003
Loading...