how to match leading '*' ??

L

Leor Zolman

(Sorry, I posted this in comp.lang.perl first before a different
newsreader showed me the existence of this sub-group; there seems to
be more activity here.)

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 "just const", rather than "*const" as
I would have hoped.

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

How am I being brain-dead?
-leor

while (<>)
{
if (/const/)
{
print "just const\n\n";
}
elsif (/\*const/) # should match leading literal '*', no???
{
print "*const\n\n";
}
else
{
print "None.\n";
}
}
 
A

Andreas Kahari

(Sorry, I posted this in comp.lang.perl first before a different
newsreader showed me the existence of this sub-group; there seems to
be more activity here.)

That group is dead.

[cut]
How am I being brain-dead?

Reverse the order of the tests.
 
L

Leor Zolman

That's because comp.lang.perl is obsolete. However I replied there.

Thanks -- I'll stick to this group now ;-)

Figures, in trying to simplify the problem (which was pure r.e.'s, no "if"
statmeents), I introduced the ordering bug, which has nothing to do with my
original problem. But at least now I do know what my original problem
is. Here's a shorter version of the test program that illustrates the issue:

while (<>)
{
$pat = "\*const";

# if (/\*const/) # OK, '*' is literal
if (/$pat/) # oops, now it's a leading r.e. '*' operator!
{
print "*const\n\n"; # should match leading literal '*', no???
}
}

The trouble is that the escaped '*' is no longer escaped when I use it in the
"if", due to the use of the variable. In fact I'm building a big, fat, complex
r.e. composed of several nested variables...and the place I need to
"escape" the '*' is in one of the "inner" ones. Any way to make that work?
Thanks,
-leor
 
A

Andreas Kahari

Leor Zolman wrote: said:
The trouble is that the escaped '*' is no longer escaped when I use it in the
"if", due to the use of the variable. In fact I'm building a big, fat, complex

Escape the * twice ("\\*") or single quote the expression ('\*').
 
G

Gunnar Hjalmarsson

Leor said:
while (<>)
{
$pat = "\*const";

That resulted in a fatal error when running your code with Perl 5.8.0:
"Quantifier follows nothing in regex; marked by <-- HERE in
m/* <-- HERE const/"

You'd better use single quotes:

$pat = '\*const';

or making the backslash literal:

$pat = "\\*const";
 
L

Leor Zolman

complex

Escape the * twice ("\\*") or single quote the expression ('\*').


Ahh, thank you. That's it. I was comparing what I'd written to other instances
where I used '\*' within single quotes -- when no variables were involved --
and didn't catch on to the implications. Now I can get back to business...
-leor
 
B

Bob Walton

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 "just const", rather than "*const" as
I would have hoped.

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

How am I being brain-dead?
-leor

while (<>)
{
if (/const/)


You need to anchor the beginning of the above pattern. As given, it
will match the string '*const', beginning at the 'c'. Like:

if (^/const/)

Or you could test for *const , then test for const once you know it's
not *const .

{
print "just const\n\n";
}
elsif (/\*const/) # should match leading literal '*', no???


Yep, if it ever got to this statement :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top