Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Can't match negated words.

by Abigail-II (Bishop)
on Jun 24, 2004 at 12:44 UTC ( #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


Comment on Re: Can't match negated words.
Download Code
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
Node Status?
node history
Node Type: note [id://369322]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-10-01 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (29 votes), past polls