How to negate specific word in regex?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
To match lines that do NOT contain a specific word, use a negative lookahead: ^(?!.*\bword\b).*$. This pattern asserts at the start of the line that the word does not appear anywhere, then matches the entire line. This works in most modern regex engines including Python, JavaScript, Java, .NET, and PCRE.
How Negative Lookahead Works
A negative lookahead (?!pattern) checks that the specified pattern does NOT match at the current position, without consuming any characters. It is a zero-width assertion, meaning it looks ahead but does not advance the regex cursor.
Breaking down ^(?!.*\bword\b).*$:
^anchors to the start of the line(?!.*\bword\b)asserts that from this position, there is no way to match any characters followed by "word" as a complete word.*$matches the entire line (only reached if the lookahead passes)
The \b word boundaries ensure you match the complete word "word" and not substrings like "wordy" or "sword".
Practical Examples by Language
Python
JavaScript
Java
grep (Command Line)
The grep -v approach is simpler and often more readable when you just need to filter lines in a file.
Negating Multiple Words
To exclude lines containing ANY of several words, chain the lookaheads:
To exclude lines containing ALL of several words (the line must contain every word to be excluded):
Negating a Word at a Specific Position
Sometimes you do not want to exclude the entire line. You want to match a word that is NOT a specific value at a certain position:
Matching Words That Do Not Start With a Prefix
Technique Comparison
| Technique | Pattern | Use Case | Engine Support |
| Negative lookahead (full line) | ^(?!.*\bword\b).*$ | Exclude lines containing a word | Python, JS, Java, .NET, PCRE |
| Negative lookahead (position) | (?!word\b)\w+ | Match any word except a specific one | Python, JS, Java, .NET, PCRE |
| grep inverse | grep -v "\bword\b" | Filter lines in files | All grep implementations |
| Character class negation | [^abc] | Exclude specific characters (NOT words) | All engines |
| Negative lookbehind | (?<!prefix)\w+ | Match words not preceded by a pattern | Python, Java, .NET (fixed-width) |
| Tempered greedy token | (?:(?!\bword\b).)* | Match text between boundaries, skipping a word | Python, Java, .NET, PCRE |
Character Class Negation vs. Word Negation
A common misconception is that [^word] negates the word "word." It does not. Character class negation [^...] only negates individual characters:
Performance Considerations
Negative lookaheads add computational cost because the regex engine must attempt to match the negated pattern at each position. For large texts, consider these optimizations:
For programmatic use where you need regex for complex pattern matching, the lookahead approach is fine for typical text sizes. For processing millions of lines, plain string methods or specialized tools like grep will outperform regex lookaheads.
Common Pitfalls
Forgetting word boundaries. Without \b, the pattern (?!.*error) also matches against substrings. The word "terrorism" contains "error"? No, but "errorless" contains "error". Using \b on both sides ensures you match whole words only.
Confusing character class negation with word negation. [^fox] does not mean "not the word fox." It means "any character that is not f, o, or x." Always use negative lookahead for word-level negation.
Missing anchors. Without ^ and $, the regex (?!.*\bfox\b).* can match partial lines, producing unexpected results. Always anchor to the line boundaries when filtering entire lines.
Case sensitivity. The pattern \bfox\b does not match "Fox" or "FOX." Add the case-insensitive flag (re.IGNORECASE in Python, /i in JavaScript) if needed.
Using negation in engines without lookahead support. Older or simpler regex engines (like POSIX BRE used in basic grep or sed without -E) do not support lookaheads. Use grep -v or grep -P (PCRE mode) as alternatives.
Over-engineering with regex. If you just need to check whether a string contains a word, a simple if "word" not in text (Python) or !text.includes("word") (JavaScript) is clearer and faster than regex.
Summary
Use ^(?!.*\bword\b).*$ with a negative lookahead to match lines that do not contain a specific word. Use \b word boundaries to avoid partial matches. Chain multiple lookaheads to exclude multiple words. For simple line filtering in files, grep -v is often a better tool than regex. Remember that [^...] negates characters, not words. For performance-sensitive applications processing large volumes of text, consider plain string methods before reaching for regex lookaheads.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.