Regular expression or operator avoids repetition

How can I use the operator or prevent repetition. In other words, the regular expression:

(word1|word2|word3)+ 

will match word1word2 , and will also match word1word1 , which I don't want, because word1 is repeated. How can I avoid a repeat?

In conclusion, I will like the following items:

 word1word2word3 word1 word2 word3word2 

Please note that they all match because there is no repetition. And I will not like the following items:

 word1word2word1 word2word2 word3word1word2word2 

Edit

Thanks @Mark I know:

 (?xi) (?: (?<A>word1|word2)(?! .* \k<A> ) # match for word1 or word2 but make sure that if you capture it it does not follow what it was just captured | (?<B>word3|word4)(?! .* \k<B> ) )+ 

because I am interested to see that something was dropping in group A or B.

+8
c # regex
source share
4 answers

You can use negative images :

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

See how it works on the Internet: rubular

+7
source share

Viewing solutions will not work in several cases, you can solve it correctly without searching queries using a construction similar to this:

 (?:(?(1)(?!))(word1)|(?(2)(?!))(word2)|(?(3)(?!))(word3))+ 

This works even if some words are substrings of others, and they will also work if you just want to find the corresponding substrings of a larger string (and not just whole lines).

Live demo .

It just works if you don’t make the changes, if it was matched earlier, done (?(1)(?!)) . (?(1)foo) is conditional and will match foo if group 1 was previously matched. (?!) always fails.

+4
source share

Byers' solution is too hardcoded and becomes quite cumbersome after increasing the letters. Why not just look for a regex to match again?

 ([^\d]+\d)+(?=.*\1) 

If this matches, this match means a repetition is found in the pattern. If the match does not work, you have a valid dataset.

0
source share

You can use a negative forward forecast containing a backlink:

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

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

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

0
source share

All Articles