Regex to match a whole string only if it lacks a given substring/suffix

前端 未结 1 1101
野趣味
野趣味 2021-01-12 07:35

I\'ve searched for questions like this, but all the cases I found were solved in a problem-specific manner, like using !g in vi to negate the regex matches,

相关标签:
1条回答
  • 2021-01-12 08:05

    Try this regular expression:

    ^(?:(?!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])))*$
    

    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.

    0 讨论(0)
提交回复
热议问题