Negative Look Ahead question?

Discussion in 'Perl Misc' started by Simon Fairey, Jun 11, 2004.

  1. Simon Fairey

    Simon Fairey Guest

    Why does the following not work unless I put foo before the (?!)
    portion, I have strings and I want to match all strings (I know
    nothing about the content of the strings) that don't contain a certain
    phrase but I have to use a negative RE rather than negating a positive
    RE

    foreach(qw(foobar fooboo)){
    print "Checking <$_>....";
    if(/(?!bar)/){
    print "Match\n";
    }else{
    print "No Match\n";
    }
    }

    I'm reading the docs but can't find out something core about the way
    regexps work
    that I'm obviously missing that will explain why this fails?

    Cheers

    Si
     
    Simon Fairey, Jun 11, 2004
    #1
    1. Advertising

  2. Simon Fairey

    Anno Siegel Guest

    Simon Fairey <> wrote in comp.lang.perl.misc:
    > Why does the following not work unless I put foo before the (?!)
    > portion, I have strings and I want to match all strings (I know
    > nothing about the content of the strings) that don't contain a certain
    > phrase but I have to use a negative RE rather than negating a positive
    > RE
    >
    > foreach(qw(foobar fooboo)){
    > print "Checking <$_>....";
    > if(/(?!bar)/){
    > print "Match\n";
    > }else{
    > print "No Match\n";
    > }
    > }


    The negative lookahead (?!bar) matches (i.e. doesn't find "bar") right
    at the beginning of the strings, so the regex engine happily reports
    a match. Look-around assertions must be anchored to some place in the
    string to be useful.

    Anno
     
    Anno Siegel, Jun 11, 2004
    #2
    1. Advertising

  3. Simon Fairey

    Ben Morrow Guest

    Quoth -berlin.de (Anno Siegel):
    > Simon Fairey <> wrote in comp.lang.perl.misc:
    > > Why does the following not work unless I put foo before the (?!)
    > > portion, I have strings and I want to match all strings (I know
    > > nothing about the content of the strings) that don't contain a certain
    > > phrase but I have to use a negative RE rather than negating a positive
    > > RE
    > >
    > > foreach(qw(foobar fooboo)){
    > > print "Checking <$_>....";
    > > if(/(?!bar)/){
    > > print "Match\n";
    > > }else{
    > > print "No Match\n";
    > > }
    > > }

    >
    > The negative lookahead (?!bar) matches (i.e. doesn't find "bar") right
    > at the beginning of the strings, so the regex engine happily reports
    > a match. Look-around assertions must be anchored to some place in the
    > string to be useful.


    If you just want the assertion '$_ does not contain bar' then you want

    if (!/bar/) {
    print "match";
    }
    else {
    print "no match";
    }

    Ben

    --
    Although few may originate a policy, we are all able to judge it.
    - Pericles of Athens, c.430 B.C.
     
    Ben Morrow, Jun 11, 2004
    #3
  4. Simon Fairey

    Guest

    (Simon Fairey) wrote:
    > Why does the following not work unless I put foo before the (?!)
    > portion,


    It works perfectly. You just don't understand what it is doing.

    > I have strings and I want to match all strings (I know
    > nothing about the content of the strings) that don't contain a certain
    > phrase but I have to use a negative RE rather than negating a positive
    > RE


    Why do you have to do it the way that doesn't work, rather than the way
    that does work? If you have artificial restrictions on what you can do,
    you had best explain what they are in more detail that this, otherwise how
    can we help?

    >
    > foreach(qw(foobar fooboo)){
    > print "Checking <$_>....";
    > if(/(?!bar)/){
    > print "Match\n";
    > }else{
    > print "No Match\n";
    > }
    > }


    While "foo" has a "bar" immediately after it, the empty string at the start
    of "foobar" does not have a "bar" immediately after it, so that empty
    string is what matches. Of course, it could find the empty string between
    f and o, or the one between o and o, if for reason it didn't stop at the
    first one.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 11, 2004
    #4
    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. cibalo
    Replies:
    2
    Views:
    197
    Rainer Weikusat
    May 23, 2013
  2. Jason Friedman

    Re: Regular expression negative look-ahead

    Jason Friedman, Jul 2, 2013, in forum: Python
    Replies:
    0
    Views:
    86
    Jason Friedman
    Jul 2, 2013
  3. Ian Kelly
    Replies:
    0
    Views:
    98
    Ian Kelly
    Jul 2, 2013
  4. Jason Friedman

    Regular expression negative look-ahead

    Jason Friedman, Jul 2, 2013, in forum: Python
    Replies:
    1
    Views:
    71
    Neil Cerutti
    Jul 2, 2013
  5. Jason Friedman

    Re: Regular expression negative look-ahead

    Jason Friedman, Jul 4, 2013, in forum: Python
    Replies:
    0
    Views:
    75
    Jason Friedman
    Jul 4, 2013
Loading...

Share This Page