Replace characters that repeats more than 20 times with 10 with a regex

136 Views Asked by At

This is basically more complicated version of this question.

Replace repeating characters with one with a regex

If there is a character that repeats more than 20 times, then replace with just ten repetition of that character.

That is, I want to replace 'adfajlkjl a sd=============================================== READFadfa' with 'adfajlkjl a sd========== READFadfa'

import re
string1 = 'adfajlkjl a sd=============================================== READFadfa'
re.sub(pattern1, r'\1\1\1\1\1\1\1\1\1\1', string1)

Output:

'adfajlkjl a sd========== READFadfa'

The above is a brute force solution using the answer in the above link. Is there another way not repeating the backreference \1 ten times?

1

There are 1 best solutions below

0
tshiono On

If your choice of programming language is Python, would you please try:

re.sub(r'(.)\1{19,}', lambda m: m.group(1) * 10, string1)

Result:

adfajlkjl a sd========== READFadfa