The notion that regex doesn't support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds:

^((?!hede).)*$

The regex above will match any string, or line without a line break, not containing the (sub)string 'hede'. As mentioned, this is not something regex is "good" at (or should do), but still, it is possible.

And if you need to match line break chars as well, use the DOT-ALL modifier (the trailing s in the following pattern):

/^((?!hede).)*$/s

or use it inline:

/(?s)^((?!hede).)*$/

(where the /.../ are the regex delimiters, i.e., not part of the pattern)

If the DOT-ALL modifier is not available, you can mimic the same behavior with the character class [\s\S]:

/^((?!hede)[\s\S])*$/

Explanation

A string is just a list of n characters. Before, and after each character, there's an empty string. So a list of n characters will have n+1 empty strings. Consider the string "ABhedeCD":

    β”Œβ”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”
S = β”‚e1β”‚ A β”‚e2β”‚ B β”‚e3β”‚ h β”‚e4β”‚ e β”‚e5β”‚ d β”‚e6β”‚ e β”‚e7β”‚ C β”‚e8β”‚ D β”‚e9β”‚
    β””β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”˜
    
index    0      1      2      3      4      5      6      7

where the e's are the empty strings. The regex (?!hede). looks ahead to see if there's no substring "hede" to be seen, and if that is the case (so something else is seen), then the . (dot) will match any character except a line break. Look-arounds are also called zero-width-assertions because they don't consume any characters. They only assert/validate something.

So, in my example, every empty string is first validated to see if there's no "hede" up ahead, before a character is consumed by the . (dot). The regex (?!hede). will do that only once, so it is wrapped in a group, and repeated zero or more times: ((?!hede).)*. Finally, the start- and end-of-input are anchored to make sure the entire input is consumed: ^((?!hede).)*$

As you can see, the input "ABhedeCD" will fail because on e3, the regex (?!hede) fails (there is "hede" up ahead!).

Top answer
1 of 16
7572

The notion that regex doesn't support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds:

^((?!hede).)*$

The regex above will match any string, or line without a line break, not containing the (sub)string 'hede'. As mentioned, this is not something regex is "good" at (or should do), but still, it is possible.

And if you need to match line break chars as well, use the DOT-ALL modifier (the trailing s in the following pattern):

/^((?!hede).)*$/s

or use it inline:

/(?s)^((?!hede).)*$/

(where the /.../ are the regex delimiters, i.e., not part of the pattern)

If the DOT-ALL modifier is not available, you can mimic the same behavior with the character class [\s\S]:

/^((?!hede)[\s\S])*$/

Explanation

A string is just a list of n characters. Before, and after each character, there's an empty string. So a list of n characters will have n+1 empty strings. Consider the string "ABhedeCD":

    β”Œβ”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”
S = β”‚e1β”‚ A β”‚e2β”‚ B β”‚e3β”‚ h β”‚e4β”‚ e β”‚e5β”‚ d β”‚e6β”‚ e β”‚e7β”‚ C β”‚e8β”‚ D β”‚e9β”‚
    β””β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”˜
    
index    0      1      2      3      4      5      6      7

where the e's are the empty strings. The regex (?!hede). looks ahead to see if there's no substring "hede" to be seen, and if that is the case (so something else is seen), then the . (dot) will match any character except a line break. Look-arounds are also called zero-width-assertions because they don't consume any characters. They only assert/validate something.

So, in my example, every empty string is first validated to see if there's no "hede" up ahead, before a character is consumed by the . (dot). The regex (?!hede). will do that only once, so it is wrapped in a group, and repeated zero or more times: ((?!hede).)*. Finally, the start- and end-of-input are anchored to make sure the entire input is consumed: ^((?!hede).)*$

As you can see, the input "ABhedeCD" will fail because on e3, the regex (?!hede) fails (there is "hede" up ahead!).

2 of 16
1001

Note that 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.

Discussions

Regex: Match word not containing - Stack Overflow
So only the top word (EFI Internal Shell) should match. ... I looked through SO and none of the answers were able to get me on the right track. For example: Regular expression that doesn't contain certain string says to use ^((?!my string).)*$ but that didn't work. Even to match any string not containing Drive. ... Your ^((?!Drive).)*$ did not work at all because you tested against a multiline input. You should use /m modifier to see what the regex ... More on stackoverflow.com
🌐 stackoverflow.com
RegEx that does NOT match desired pattern
Hi all, I was looking into using the RegEx Actions to match multiple times - and that lead me to Peter's post: So I did what Peter suggested. That is working fine. But my search also lead me to this post by @JMichaelTX It seems like this would be more efficient or possible slightly quicker. More on forum.keyboardmaestro.com
🌐 forum.keyboardmaestro.com
12
0
June 19, 2020
Match EVERYTHING except one word
Your description doesn't seem to match the example. I assume you don't want Bob? A match needs to be continuous. At best you can match everything before and everything after in separate matches. But it might be easier to remove the word you don't want from the string (i.e. match only that word and then remove it). What do you want to do with the text? More on reddit.com
🌐 r/regex
3
7
May 11, 2021
regex - Regular expression not matching specific string - Stack Overflow
I have this: /\w+.\w* to match two words separated by a dot, but I don't know how to add 'and do not match .action'. ... Firstly, you need to escape your . character as that's taken as any character in Regex. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Sentry
sentry.io β€Ί sentry answers β€Ί linux β€Ί write a regular expression to match lines not containing a word
Write a regular expression to match lines not containing a word | Sentry
If β€œword” is found, discard the current match, otherwise continue evaluating the expression. ... m: Multiline flag. Use ^ and $ to match the start and end of a line, rather than the start and end of the whole string. Notably, this expression will not match empty lines. If this behavior is desired, we can add the s (dotall) flag, which will make . match all characters including newlines: ... If the s flag is not supported by your regex implementation, you can replace .
🌐
O'Reilly
oreilly.com β€Ί library β€Ί view β€Ί regular-expressions-cookbook β€Ί 9781449327453 β€Ί ch05s04.html
5.4. Find All Except a Specific Word - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - Although a negated character class (written as β€Ή[^β‹―]β€Ί) makes it easy to match anything except a specific character, you can’t just write β€Ή[^cat]β€Ί to match anything except the word cat. β€Ή[^cat]β€Ί is a valid regex, but it matches any character except c, a, or t.
Authors: Jan GoyvaertsSteven Levithan
Published: 2012
Pages: 609
🌐
O'Reilly
oreilly.com β€Ί library β€Ί view β€Ί regular-expressions-cookbook β€Ί 9780596802837 β€Ί ch05s05.html
5.5. Find Any Word Not Followed by a Specific Word - Regular Expressions Cookbook [Book]
May 22, 2009 - You want to match any word that is not immediately followed by the word cat, ignoring any whitespace, punctuation, or other nonword characters that appear in between.
Authors: Jan GoyvaertsSteven Levithan
Published: 2009
Pages: 510
🌐
Regex Tester
regextester.com β€Ί 15
Match string not containing string - Regex Tester/Debugger
Regular Expression to Given a list of strings (words or other characters), only return the strings that do not match.
🌐
Wyssmann Engineering
wyssmann.com β€Ί adrian's tech kb & blog β€Ί blog β€Ί regular expression ignore or exclude a specific word, find everything else
Regular Expression - ignore or exclude a specific word, find everything else | Adrian's Tech KB & Blog
March 24, 2021 - Similar to positive lookahead, except that negative lookahead only succeeds if the regex inside the lookahead fails to match. So the final solution using the techniques mentioned above ... None the word we want to β€œignore” i.e. should not match
Find elsewhere
🌐
Lineserve
lineserve.net β€Ί home β€Ί blog β€Ί complete guide: regex to match lines not containing a word
Complete Guide: Regex to Match Lines Not Containing a Word β€” Lineserve Blog
July 5, 2026 - Match lines that do not contain a specific word using regex negative lookaheads like (?!.*word), avoiding grep -v, with working examples across multiple languages and tools.
🌐
O'Reilly
oreilly.com β€Ί library β€Ί view β€Ί regular-expressions-cookbook β€Ί 9781449327453 β€Ί ch05s11.html
5.11. Match Complete Lines That Do Not Contain a Word - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - ... In order to match a line that does not contain something, use negative lookahead (described in Recipe 2.16). Notice that in this regular expression, a negative lookahead and a dot are repeated together using a noncapturing group.
Authors: Jan GoyvaertsSteven Levithan
Published: 2012
Pages: 609
🌐
Regex101
regex101.com β€Ί r β€Ί yN7tT8 β€Ί 1
regex101: Matches when the string does not contain an exact word
An explanation of your regex will be automatically generated as you type. Detailed match information will be displayed here automatically.
🌐
Keyboard Maestro
forum.keyboardmaestro.com β€Ί questions & suggestions
RegEx that does NOT match desired pattern - Questions & Suggestions - Keyboard Maestro Discourse
June 19, 2020 - Hi all, I was looking into using the RegEx Actions to match multiple times - and that lead me to Peter's post: So I did what Peter suggested. That is working fine. But my search also lead me to this post by @JMichaelTX It seems like this would be more efficient or possible slightly quicker.
🌐
O'Reilly
oreilly.com β€Ί library β€Ί view β€Ί regular-expressions-cookbook β€Ί 9781449327453 β€Ί ch05s06.html
5.6. Find Any Word Not Preceded by a Specific Word - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - You want to match any word that is not immediately preceded by the word cat, ignoring any whitespace, punctuation, or other nonword characters that come between. Lookbehind lets you check if text appears before a given position. It works by instructing the regex engine to temporarily step backward ...
Authors: Jan GoyvaertsSteven Levithan
Published: 2012
Pages: 609
🌐
Squash
squash.io β€Ί how-to-regex-regex-not-match
Using Regular Expressions to Exclude or Negate Matches
September 12, 2023 - This allows you to match any pattern except the ones specified. For example, the regex pattern ^(?!dog$|cat$) matches any word that is not exactly "dog" or "cat".
🌐
Regular-Expressions.info
regular-expressions.info β€Ί wordboundaries.html
Regex Tutorial: \b Word Boundaries
The regex \b\w+\b thus finds 5 words in this string: John, s, mother, in, and law. If the flavor matches Unicode β€œletters” as word characters then \b does not match between ideographs and similar characters that Unicode treats as β€œother letters”. So \b can only match at the start and the end of the string 中文单词.
🌐
Baeldung
baeldung.com β€Ί home β€Ί algorithms β€Ί searching β€Ί regular expression to match text without a certain word
Regular Expression to Match Text Without a Certain Word | Baeldung on Computer Science
March 18, 2024 - And we want to know if the text doesn’t include the word β€œfounder” or not. The regular expression algorithm should return false. A regular expression is a string of literals and metacharacters. A metacharacter is a character that guides the regular expression matching method.
🌐
Notepad++ Community
community.notepad-plus-plus.org β€Ί topic β€Ί 16835 β€Ί regex-match-string-not-containing-string
regex: Match string not containing string | Notepad++ Community
December 29, 2018 - Remark : Of course, for correct testing of these regexes, just copy the text provided, in that way : TEST : I love you. TEXT : She loves me. ABCD : It is not about me. TEXT : You love her. Statement "TEST" : I love you. Statement "TEXT" : She loves me. Statement "ABCD" : It is not about me. Statement "TEXT" : You love her. I love you. = TEST She loves me. = TEXT It is not about me. = ABCD You love her. = TEXT Β· Now, to match all complete non-empty lines which do NOT contain the expression <p class="TEXT">, possibly preceded by some blank characters, with that exact case, use the regex :