python regular expression letters in a row

expression, python, regex

Solution

The easiest way of doing this would be using a backreference twice:

r"([a-zA-Z])\1\1"

This checks for the opposite of what you're asking for, so negate the result. If you're using this as a part of a larger regex, remember to change the backreference index if needed.

Problem

I would like to write a regular expression in Python that checks if a string does not contain more than 2 same letters in a row, e.g. wood valid, woood not valid I tried it with ``` [a-zA-z]{,2} ``` but that does not work

Original source