Say that the user inputs:
\"daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm\"
How would you go about finding the highest
Starting with the input string:
input = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"
To get the max consecutive number of occurrences, you would use:
max(len(s) for s in re.findall(r'a+', input))
To replace only the longest unbroken sequence of "a"s with 2 "a"s, you would use:
maxMatch = max(re.finditer(r'a+', input), key= lambda m: len(m.group()))
output = input[:maxMatch.start()] + "aa" + input[maxMatch.end():]
First, I obtain an iterable of MatchObjects by testing the input string against the regex a+, then use max to obtain the MatchObject with the greatest length. Then, I splice the portion of the original string up to the start of the match, the string "aa", and the portion of the original string after the end of the match to give you your final output.
To replace all occurrences of more than 2 "a"s with 2 "a"s, you would use:
output = re.sub(r'a{3,}', "aa", input)