Python look-behind regex "fixed-width pattern" error while looking for consecutive repeated words

Viewed 331

I have a text with words separated by ., with instances of 2 and 3 consecutive repeated words:

My.name.name.is.Inigo.Montoya.You.killed.my.father.father.father.Prepare.to.die-

I need to match them independently with regex, excluding the duplicates from the triplicates.

Since there are max. 3 consecutive repeated words, this

r'\b(\w+)\.+\1\.+\1\b'

successfully catches

father.father.father

However, in order to catch 2 consecutive repeated words, I need to make sure the next and previous words aren't the same. I can do a negative look-ahead

r'\b(\w+)\.+\1(?!\.+\1)\b'

but my attempts at the negative look-behind

r'(?<!(\w)\.)\b\1\.+\1\b(?!\.\1)'

either return a fixed-width issue (when I keep the +) or some other issue.

How should I correct the negative look-behind?

2 Answers
Related