How can I use the or
operator while not allowing repetition? In other words the regex:
(word1|word2|word3)+
will match wo
The lookahead solutions will not work in several cases, you can solve this properly, without lookarounds, by using a construct like this:
(?:(?(1)(?!))(word1)|(?(2)(?!))(word2)|(?(3)(?!))(word3))+
This works even if some words are substrings of others and will also work if you just want to find the matching substrings of a larger string (and not only match whole string).
Live demo.
It simply works by failing the alteration if it has been matched previously, done by (?(1)(?!))
. (?(1)foo)
is a conditional, and will match foo
if group 1
has previously matched. (?!)
always fails.