You are fundamentally correct, but [^b]
will still match o
and g
in bog
-- meaning it is a successful match, even though it didn't match the whole string. [^bog]
will only match h
in hog
, d
in dog
, and nothing in bog
-- meaning it doesn't match bog
.
I think this will make more sense if you look at ^[^b]+$
. This will match 1+
non-b
characters, anchored at the beginning (^
) and end ($
) of the string. Comparing that to your initial expression of [^b]
or [^bog]
, you can see the difference. I suggest using a GUI RegEx tester (the previously linked one is my favorite), which will really help illustrate the logic.
[^b]
matches a character that is not ab
. So in yourhog / bog / dog
example, it matches all of them since all words have a letter in them that is not ab
.[^bog]
actually matches any character that is neither ab
, noro
, norg
, that's why it does not match any letter ofbog
. Neither would it matchgob
orogb
etc. The answer to that question, by the way, could just be[dh]og
to matchdog
,hog
, but notbog
.