Negative Look Ahead question?

S

Simon Fairey

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
 
A

Anno Siegel

Simon Fairey said:
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
 
B

Ben Morrow

Quoth (e-mail address removed)-berlin.de (Anno Siegel):
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
 
C

ctcgag

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top