how to match leading '*' ??

Discussion in 'Perl Misc' started by Leor Zolman, Sep 27, 2003.

  1. Leor Zolman

    Leor Zolman Guest

    (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";
    }
    }
     
    Leor Zolman, Sep 27, 2003
    #1
    1. Advertising

  2. 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
     
    Andreas Kahari, Sep 27, 2003
    #2
    1. Advertising

  3. Leor Zolman

    Leor Zolman Guest

    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
    >
     
    Leor Zolman, Sep 27, 2003
    #3
  4. 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
     
    Andreas Kahari, Sep 27, 2003
    #4
  5. 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
     
    Gunnar Hjalmarsson, Sep 27, 2003
    #5
  6. Leor Zolman

    Leor Zolman Guest

    In article <>,
    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
     
    Leor Zolman, Sep 27, 2003
    #6
  7. 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
     
    Gunnar Hjalmarsson, Sep 27, 2003
    #7
  8. Leor Zolman

    Bob Walton Guest

    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
     
    Bob Walton, Sep 27, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leor Zolman

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

    Leor Zolman, Sep 27, 2003, in forum: Perl
    Replies:
    1
    Views:
    385
    John Bokma
    Sep 27, 2003
  2. hiwa
    Replies:
    0
    Views:
    658
  3. Victor
    Replies:
    2
    Views:
    669
    Victor
    May 17, 2004
  4. John Nagle
    Replies:
    4
    Views:
    619
    James Antill
    Aug 7, 2007
  5. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    582
    Delaney, Timothy (Tim)
    Aug 7, 2007
Loading...

Share This Page