A regular expression matching the word NOT inside a certain number of words of another word

Hope I can explain this.

I have a regex for matching two words side by side. For example, if I want to find the word "account" and "number" within 5 words of each other:

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

This works great.

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

For example, I need a regular expression that matches "Butthead", but only if "Beavis" is not within 2 words, either before or after Butthead.

So Butthead and Beavis are not the same. Beavis and Butthead do not match. But Beavis Sure Giant Butthead will match, because Beavis and Butthead are NOT 2 words.

+4
source share
3 answers

This should work if your regexp system supports negative variable length look. I do not think many regex engines support this. I know perl and php do not yet support this. I have not been able to test since I use perl and php to test regular expressions.

/ (<Beavis (?!? \ S + \ W +) \ S +) Butthead (?!? (: \ S + \ W +) Beavis) /

+1
source

Can't you make two matches? Match to find the origin of the word anywhere (easily), then discard this match if the word is not next to another word (you already have a solution for this).

0
source

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

maybe something like this ... did not try, though ... basically I tried my way using the following logic: NOT ((contains Butthead 2 words Beavis) OR (contains Beavis 2 words Butthead))

0
source

All Articles