RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a line that does not contain a word

/^(?!.*\bERROR\b).*$/m

A negative lookahead at the start asserts that the forbidden word appears nowhere ahead, then .* consumes the line. This is how you express 'not' in a language that has no not-operator for whole patterns.

What each part does

(?! … )negative lookahead — fails if the inside matches
.*\bERROR\bthe word anywhere on the line
.*$having passed the check, match the line itself

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
all goodmatchesall good
warning: disk lowmatcheswarning: disk low
no problemsmatchesno problems
ERROR: disk fullno match
fatal ERROR hereno match

The mistake to avoid

Writing [^ERROR] and expecting it to exclude the word. A character class excludes characters, not sequences — that one rejects any line containing E, R or O.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers