Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matching 3 or more of the same character in python

Tags:

python

regex

I'm trying to use regular expressions to find three or more of the same character in a string. So for example: 'hello' would not match 'ohhh' would.

I've tried doing things like:

re.compile('(?!.*(.)\1{3,})^[a-zA-Z]*$') 
re.compile('(\w)\1{5,}')

but neither seem to work.

like image 615
Adam Avatar asked Jun 29 '11 09:06

Adam


2 Answers

(\w)\1{2,} is the regex you are looking for.

In Python it could be quoted like r"(\w)\1{2,}"

like image 60
Qtax Avatar answered Sep 27 '22 02:09

Qtax


if you're looking for the same character three times consecutively, you can do this:

(\w)\1\1

if you want to find the same character three times anywhere in the string, you need to put a dot and an asterisk between the parts of the expression above, like so:

(\w).*\1.*\1

The .* matches any number of any character, so this expression should match any string which has any single word character that appears three or more times, with any number of any characters in between them.

Hope that helps.

like image 43
Spudley Avatar answered Sep 23 '22 02:09

Spudley