Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Can't match negated words.

by Abigail-II (Bishop)
on Jun 24, 2004 at 12:44 UTC ( [id://369322]=note: print w/replies, xml ) Need Help??


in reply to Can't match negated words.

If you just want to match a line where a particular word doesn't appear in, !~ does the trick. But if "not this word" is part of a regular expression, the !~ will not do it. Instead, you basically have to "progress carefully", looking ahead on each step on your way. That is, match a character (any character) after you've concluded it doesn't start a forbidden word. If no character in the (sub)string you match doesn't start a forbidden word, no forbidden words will be in the matched string. How do you check? Use negative lookahead:
/^(?:(?!throw).)*$/s

Abigail

Replies are listed 'Best First'.
Re^2: Can't match negated words.
by perlgags78 (Acolyte) on Jun 24, 2004 at 12:52 UTC
    Hi folks, Thanks for all the replies!! This is great. I'm used to forums that take ages or never get back to you.. Abigail, ye've nailed it. That's exactly what I'm looking for, where "not throw" is part of the expression. I'm not familiar with the look ahead stuff but I've a couple of places with examples and I'll look at them. Incidentally are there any online resources you guys could recommend? Thanks again everyone, Mark.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://369322]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-29 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found