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!).
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!).
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.
A great way to do this is to use negative lookahead:
^(?!.*bar).*$
The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead [is any regex pattern].
Solution:
^(?!.*STRING1|.*STRING2|.*STRING3).*$
xxxxxx OK
xxxSTRING1xxx KO (is whether it is desired)
xxxSTRING2xxx KO (is whether it is desired)
xxxSTRING3xxx KO (is whether it is desired)
Regex: Match word not containing - Stack Overflow
RegEx that does NOT match desired pattern
Match EVERYTHING except one word
regex - Regular expression not matching specific string - Stack Overflow
Hello, I would like to match everything except one particular word (let's say Clide) in a string.
Alice Bob Clide to Alice Clide
The problem is that example I found online don't match the string at all if it contain the word.
I would still like to match what's before and after the word except itself.
Is that possible please?
Firstly, you need to escape your . character as that's taken as any character in Regex.
Secondly, you need to add in a Match if suffix is not present group - signified by the (?!) syntax.
You may also want to put a circumflex ^ to signify the start of a new line and change your * (any repetitions) to a + (one or more repititions).
^/\w+\.(?!action)\w+ is the finished Regex.
^\w+\.(?!action)\w*