Using a character class such as [^ab]
will match a single character that is not within the set of characters. (With the ^
being the negating part).
To match a string which does not contain the multi-character sequence ab
, you want to use a negative lookahead:
^(?:(?!ab).)+$
And the above expression disected in regex comment mode is:
(?x) # enable regex comment mode
^ # match start of line/string
(?: # begin non-capturing group
(?! # begin negative lookahead
ab # literal text sequence ab
) # end negative lookahead
. # any single character
) # end non-capturing group
+ # repeat previous match one or more times
$ # match end of line/string
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…