How can I use the or
operator while not allowing repetition? In other words the regex:
(word1|word2|word3)+
will match wo
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
.