Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regular expression letters in a row

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

like image 958
wasp256 Avatar asked Sep 20 '26 16:09

wasp256


1 Answers

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.

like image 159
Matti Virkkunen Avatar answered Sep 22 '26 07:09

Matti Virkkunen