Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Regular expression to match a line that doesn't contain a word

I realize it's feasible to coordinate with a word and afterwards switch the matches utilizing different tools (for example grep - v). In any case, is it possible to coordinate with lines that don't contain a particular word, for example, hede, utilizing a standard expression?
Input:*
*hoho
hihi
haha
hede

*Code:*
grep "<Regex for 'doesn't contain hede'>" input

*Desired output:*
hoho
hihi
haha
by

2 Answers

ninja01
the solution to does not start with “hede”:

^(?!hede).$

is generally much more efficient than the solution to does not contain “hede”:

^((?!hede).)
$

The former checks for “hede” only at the input string’s first position, rather than at every position.
sandhya6gczb
If you want the regex test to only fail if the entire string matches, the following will work:

^(?!hede$).*

For exact matching use this

myStr !== 'foo'

Login / Signup to Answer the Question.