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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leor Zolman

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


    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";
    }
    }

  • John Bokma

    #2
    Re: match leading '*' in r.e... how?

    Leor Zolman wrote:
    [color=blue]
    > 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";[/color]

    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).
    [color=blue]
    > }
    > elsif (/\*const/)
    > {
    > print "*const\n\n ";
    > }
    > else
    > {
    > print "None.\n";
    > }
    > }[/color]

    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

    Comment

    Working...