Matching all words except one
Try:
\b(?!boy\b).*?\b
which means:
- Zero width word break (
\b
) - That isn't followed by "boy" and another word break;
- followed by any characters in a non-greedy way;
- Up until another word break.
Note: the word break matches the start of the string, the end of the string and any transition from word (number, letter or underscore) to non-word character or vice versa.
If you use "boy" as splitter, you would get remaining parts. You could use those as selection keys.
>>> re.split("boy","I am a good buy and bad boy too")
['I am a good buy and bad ', ' too']
/\b(?!boy)\S+/g
You can use negative look behind:
\w+\b(?<!\bboy)
Or negative look ahead since not all support negative look behind
(?!boy\b)\b\w+
You can read about negative look ahead here