Regular Expression to exclude set of Keywords

后端 未结 7 1725
一个人的身影
一个人的身影 2020-11-30 22:54

I want an expression that will fail when it encounters words such as \"boon.ini\" and \"http\". The goal would be to take this expression and be able to construct for any se

7条回答
  •  自闭症患者
    2020-11-30 23:41

    An alternative expression that could be used:

    ^(?!.*IgnoreMe).*$
    

    ^ = indicates start of line
    $ = indicates the end of the line
    (?! Expression) = indicates zero width look ahead negative match on the expression

    The ^ at the front is needed, otherwise when evaluated the negative look ahead could start from somewhere within/beyond the 'IgnoreMe' text - and make a match where you don't want it too.

    e.g. If you use the regex:

    (?!.*IgnoreMe).*$
    

    With the input "Hello IgnoreMe Please", this will will result in something like: "gnoreMe Please" as the negative look ahead finds that there is no complete string 'IgnoreMe' after the 'I'.

提交回复
热议问题