Feb-22-2017, 02:45 AM
check this one, I think this should speed it up, because the each |BS| group and preceding chars are replaced in one
re.sub
#!/usr/bin/python3 import re strings = ['it |BS||BS||BS|this is one|BS||BS||BS|an example', 'it |BS|this is an example', 'it |BS||BS|this is an example', 'it |BS||BS||BS|this is an example', 'it |BS||BS||BS||BS|this is an example', 'this one|BS||BS||BS||BS|it |BS||BS||BS||BS|'] ptrn = re.compile(r'(\|BS\|)+') for string in strings: print(string) while True: match = re.search(ptrn, string) if match: num_chars = min(match.start(), int(len(match.group())/4)) sub_pattern = re.compile(r'[\w ]{{{}}}(\|BS\|)+'.format(num_chars)) string = sub_pattern.sub('', string, count=1) else: break print(string) print('\n')Also note the last test string, it's a border case when later |BS| group will delete chars preceding previous |BS| group.