469,898 Members | 1,586 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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

Jul 19 '05 #1
1 1393
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

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Ravi | last post: by
19 posts views Thread by Tom Deco | last post: by
5 posts views Thread by GarryJones | last post: by
2 posts views Thread by Jon Slaughter | last post: by
6 posts views Thread by JimmyKoolPantz | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.