Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Negative Look Ahead question?

Reply
Thread Tools

Negative Look Ahead question?

 
 
Simon Fairey
Guest
Posts: n/a
 
      06-11-2004
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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-11-2004
Simon Fairey <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      06-11-2004

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel):
> Simon Fairey <(E-Mail Removed)> 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.
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      06-11-2004
(E-Mail Removed) (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
 
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
Can I use a look-ahead and a look-behind at the same time? dan.j.weber@gmail.com Perl Misc 4 04-09-2008 10:25 PM
The Road Ahead - a look back on bit-tech.net Silverstrand Front Page News 0 02-09-2006 02:56 AM
Why do look-ahead and look-behind have to be fixed-width patterns? inhahe Python 3 01-28-2005 12:50 PM
GO AHEAD -MAKE ME LOOK DUMB- Please Aubrey Hutchison Python 4 12-31-2003 06:38 PM
File processing: Line Look-ahead Prabh Java 1 09-10-2003 11:13 PM



Advertisments