Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > match leading '*' in r.e... how?

Reply
Thread Tools

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

 
 
Leor Zolman
Guest
Posts: n/a
 
      09-27-2003

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

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      09-27-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
All leading tabs or all leading spaces - why isn't that enforced? John Nagle Python 4 08-07-2007 04:05 PM
RE: All leading tabs or all leading spaces - why isn't that enforced? Delaney, Timothy (Tim) Python 0 08-07-2007 03:50 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM
how to match leading '*' ?? Leor Zolman Perl Misc 7 09-27-2003 08:36 PM



Advertisments