Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to match leading '*' ??

Reply
Thread Tools

how to match leading '*' ??

 
 
Leor Zolman
Guest
Posts: n/a
 
      09-27-2003
(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";
}
}
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      09-27-2003
In article < >, 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
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      09-27-2003
In article <>,
d 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
>


 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      09-27-2003
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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-27-2003
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

 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      09-27-2003
In article <slrnbnbgji.h6k.ak+> ,
ak+ 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


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

 
Reply With Quote
 
Bob Walton
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 "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

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
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
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM
match leading '*' in r.e... how? Leor Zolman Perl 1 09-27-2003 04:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57