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

  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. 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. hiwa
    Replies:
    0
    Views:
    636
  2. Victor
    Replies:
    2
    Views:
    642
    Victor
    May 17, 2004
  3. John Nagle
    Replies:
    4
    Views:
    591
    James Antill
    Aug 7, 2007
  4. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    567
    Delaney, Timothy (Tim)
    Aug 7, 2007
  5. Leor Zolman

    how to match leading '*' ??

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

Share This Page