Regex to match all words except a given list

后端 未结 6 937
Happy的楠姐
Happy的楠姐 2020-12-08 21:15

I am trying to write a replacement regular expression to surround all words in quotes except the words AND, OR and NOT.

I have tried the following for the match par

6条回答
  •  星月不相逢
    2020-12-08 21:58

    John,

    The regex in your question is almost correct. The only problem is that you put the lookahead at the end of the regex instead of at the start. Also, you need to add word boundaries to force the regex to match whole words. Otherwise, it will match "nd" in "and", "r" in "or", etc, because "nd" and "r" are not in your negative lookahead.

    (?i)\b(?!and|not|or)(?[a-z0-9]+)\b

提交回复
热议问题