tags:

views:

63

answers:

3

Hope I can explain this one.

I've got a regex for matching two words near each other. For example, if I want to find the word "account" and "number" within 5 words of each other:

\baccount\W+(?:\w+\W+){1,6}?number\b

This works perfectly.

Now I need to find a way to search for a word as long as it is NOT within 2 words of another word.

For example, I need a regex that matches "Butthead" but only if "Beavis" is not within 2 words, either BEFORE OR AFTER Butthead.

So Butthead and Beavis would not match. Beavis and Butthead would not match. But Beavis Sure Is a Giant Butthead would match because Beavis and Butthead are NOT within 2 words.

A: 

Can't you just do two matches? Match to find the occurence of the word anywhere (easy) then discard that match if the word is not near the other word (you already have a solution for that).

Paul
I don't think I can, because I'm doing this within a "Rules Engine" of an e-mail filtering system which filters emails based on certain criteria, so I need a single regex to define the critera.
Jason
If the word occurs both near and not-near the other word, I want it to not match, because it does occur near and that's the important thing.
Jason
A: 

((?!((\Butthead\W+(?:\w+\W+){1,2}?Beavis\b)|(\Beavis\W+(?:\w+\W+){1,2}?Butthead\b))).)*

maybe something like this... didn't try it though... basically i've tryed your way using the following logic: NOT( (contains Butthead 2 words Beavis) OR (contains Beavis 2 words Butthead) )

Flo.
+1  A: 

This should work if your regexp system supports variable length negative look behinds. I do not think many regex engines support this yet. I know that perl and php do not yet support this. I was not able to test since I use perl and php for my regex testing.

/(?<!beavis(?:\s+\w+)?\s+)butthead(?!(?:\s+\w+)?beavis)/

CtRanger
Without a negative look behind I cannot see an alternative way too match this properly. The proposal submitted by Flo works except for the case where butthead occurs on a line by itself with no beavis present.
CtRanger