sura
sura

Reputation: 1511

Identifying repetition of characters using regular expressions

I want to find repetitions of individual character, where the character repeats for 3 or more times. "444", "sss", and "------" should be identified, while "dd" should not be identified.

I came across a previous StackOverflow question which gave the solution as:

regexp = re.compile(r"(.)\1")

However, this identifies 2 or more repetitions of the same character. I tried (r"(.)\{3,}") which does not work, and (r"(.)\3") which gives an error. Can somebody please give me the correct regex for this?

Upvotes: 2

Views: 1750

Answers (1)

Borealid
Borealid

Reputation: 98459

You want (r"(.)\1\1") (or even (r"(.)\1\1+")).

The regex is "match any character (.), then match that same character again (\1), then match that same character a third time (for the first variant I've got here) or match that same character one or more additional times (for the second variant above).

Upvotes: 5

Related Questions