Regex with exception of particular words

前端 未结 6 1386
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 03:45

I have problem with regex. I need to make regex with an exception of a set of specified words, for example: apple, orange, juice. and given these words, it will match everyt

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 04:16

    \A(?!apple\Z|juice\Z|orange\Z).*\Z
    

    will match an entire string unless it only consists of one of the forbidden words.

    Alternatively, if you're not using Ruby or you're sure that your strings contain no line breaks or you have set the option that ^ and $ do not match on beginnings/ends of lines

    ^(?!apple$|juice$|orange$).*$
    

    will also work.

提交回复
热议问题