> I want to match "XIX" but not "XXX" or "WOW" but not "WWW". This seems to do the trick: import re matcher = re.compile(r'(.)(?!\1).\1').match print matcher('XAX') print matcher('XXX') Alex.