def fixer(match):
    ones = match.group(0)
if len(ones) < THRESHOLD:
    return '0' * len(ones)
return ones
my_string = (
    '00000001111100011110001111100001110000001000001111000000111001111000')
print(re.sub('(1+)', fixer, my_string))
Regex to remove bit signal noise spikes