Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   how to match leading '*' ?? (http://www.velocityreviews.com/forums/t882773-how-to-match-leading.html)

Leor Zolman 09-27-2003 04:03 PM

how to match leading '*' ??
 
(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";
}
}

Andreas Kahari 09-27-2003 04:46 PM

Re: how to match leading '*' ??
 
In article <d38469a3.0309270803.47d0133f@posting.google.com >, Leor Zolman wrote:
> (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.



--
Andreas Kähäri

Leor Zolman 09-27-2003 04:54 PM

Re: how to match leading '*' ??
 
In article <1064679503.9206@halkan.kabelfoon.nl>,
postmaster@castleamber.com.invalid says...
>
>Leor Zolman wrote:
>
>> (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'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




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



Andreas Kahari 09-27-2003 05:01 PM

Re: how to match leading '*' ??
 
In article <thjdb.599479$Ho3.116667@sccrnsc03>, Leor Zolman wrote:
[cut]
> 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 ('\*').


--
Andreas Kähäri

Gunnar Hjalmarsson 09-27-2003 05:07 PM

Re: how to match leading '*' ??
 
Leor Zolman wrote:
>
> 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";

> # 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???
> }
> }


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Leor Zolman 09-27-2003 05:13 PM

Re: how to match leading '*' ??
 
In article <slrnbnbgji.h6k.ak+usenet@vinland.freeshell.org> ,
ak+usenet@freeshell.org says...
>
>In article <thjdb.599479$Ho3.116667@sccrnsc03>, Leor Zolman wrote:
>[cut]
>> 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 ('\*').



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


>
>
>--
>Andreas Kähäri



Gunnar Hjalmarsson 09-27-2003 08:35 PM

Re: how to match leading '*' ??
 
Bob Walton wrote:
> You need to anchor the beginning of the above pattern. ... Like:
>
> if (^/const/)


Suppose you mean:

if (/^const/)

;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Bob Walton 09-27-2003 08:36 PM

Re: how to match leading '*' ??
 
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 :-)


> {
> print "*const\n\n";
> }
> else
> {
> print "None.\n";
> }
> }
>


--
Bob Walton



All times are GMT. The time now is 10:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.