Feb-22-2017, 12:45 AM
Thanks for all your reply. After testing them all, I ended up using buran's code which is the faster and work as expected.
buran: 2.6e-06s
wavic: 2.1e-05s (infinite loop if the string begin with |BS|)
ofnuts: 6.3e-05s
buran: 2.6e-06s
wavic: 2.1e-05s (infinite loop if the string begin with |BS|)
ofnuts: 6.3e-05s
pattern = re.compile(r'[\w ]?\|BS\|') buffer = "it BS|BS||BS||BS||BS||BS|this is o n |BS||BS||BS||BS||BS|an example" while True: after_sub = pattern.sub('', buffer, count=1) if buffer == after_sub: break else: buffer = after_sub print(buffer)