Regex 'or' operator avoid repetition

后端 未结 4 2330
轮回少年
轮回少年 2021-02-20 03:16

How can I use the or operator while not allowing repetition? In other words the regex:

(word1|word2|word3)+

will match wo

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-20 03:47

    You could use negative lookaheads:

    ^(?:word1(?!.*word1)|word2(?!.*word2)|word3(?!.*word3))+$
    

    See it working online: rubular

提交回复
热议问题