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\b | the 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.
| Input | Result | Matched |
|---|---|---|
| all good | matches | all good |
| warning: disk low | matches | warning: disk low |
| no problems | matches | no problems |
| ERROR: disk full | no match | — |
| fatal ERROR here | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID