I found many questions regarding this on SO, but they always assume that the word is at the beginning or end.
Assuming the word is cat, I don't want to match any of these:
- cat
- hellocat
- catfood
- nocatthere
A common idea seems to be something like this:
^([^c]*|c([^a]|$|a([^t]|$)))*
The regex works like this:
match anything except a c
[^c]*
or, if you have matched a c, then match only something other than a, or the end of the word
([^a]|$
or, if you have matched a ca, then match only something other than t, or the end of the word
([^t]|$)
The examples work correctly, they aren't matched.
But the regex can be confused:
The following examples match:
- ccat
- cacat