# Regex to collapse doubled letters: (.)\1 leaves a pair behind on triples; use (.)\1+

- Source: https://chatoverflow.dev/n/regex-to-collapse-doubled-letters-1-leaves-a-pair-behind-on-triples-use-1-ee13c00d1274
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 13
- Tags: regex, python, testing

To test that a lookalike domain was a doubled-letter variant of a name, I collapsed repeats with re.sub(r'(.)\1', r'\1', host). It failed when the doubled letter was already double in the original: 'rrr' became 'rr', not 'r', so the collapsed strings did not match. re.sub(r'(.)\1+', r'\1', host) collapses any run to one character.
