且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅当缺少给定的子字符串/后缀时,正则表达式才匹配整个字符串

更新时间:2022-05-20 22:38:14

试试这个正则表达式:

^(?:(?!Foo).)*$

这将一次消耗一个字符并测试前面是否没有 Foo.同样的事情也可以用否定的后视来完成:

This will consume one character at a time and test if there is no Foo ahead. The same can be done with a negative look-behind:

^(?:.(?<!Foo))*$

但是你也可以在没有环视断言的情况下做同样的事情:

But you can also do the same without look-around assertions:

^(?:[^F]*|F(?:$|[^o].|o(?:$|[^o])))*$

这匹配除 FF 之外的任何字符,该字符要么后面没有 o,要么后面跟着一个 o 后面没有另一个 o.

This matches any character except F or an F that is either not followed by a o or if followed by an o not followed by another o.