使用类似的字符类[^ab]将匹配不在字符集内的单个字符。(^作为否定的部分)。要匹配不包含多字符序列的字符串ab,您需要使用否定前瞻:^(?:(?!ab).)+$并且正则表达式注释模式中的上述表达是:(?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