Regex 'or' operator avoid repetition

后端 未结 4 2329
轮回少年
轮回少年 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:39

    You could use a negative look-ahead containing a back reference:

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

    where \1 refers to the match of the capture group (word1|word2|word3).

    Note that this assumes word2 cannot be formed by appending characters to word1, and that word3 cannot be formed by appending characters to word1 or word2.

提交回复
热议问题