Hello,
I made this script to find all "|BS|" blocks in a string, and simulate a backspace, as if the string would be typed in a linetext input field. I came up with this:
It works, but feels very inefficient and I would like to improve it, avoiding the needs for an array or using a single pass regex. I looked at the documentation for re but yet I could not find a satisfying answer. Hopefully you can give me some tips![Big Grin Big Grin](https://python-forum.io/images/smilies/biggrin.png)
Thanks!
I made this script to find all "|BS|" blocks in a string, and simulate a backspace, as if the string would be typed in a linetext input field. I came up with this:
#!/usr/bin/python3 import re if __name__ == '__main__': string = "it |BS||BS||BS|this is one|BS||BS||BS|an example" #|BS| as in Backspace while re.search("\|BS\|", string): array = list(string) for m in re.finditer("\|BS\|", string): del array[m.start():m.end()] if m.start()-1 >= 0: del array[m.start()-1] string = ''.join(array) break print(string)which output "this is an example"
It works, but feels very inefficient and I would like to improve it, avoiding the needs for an array or using a single pass regex. I looked at the documentation for re but yet I could not find a satisfying answer. Hopefully you can give me some tips
![Big Grin Big Grin](https://python-forum.io/images/smilies/biggrin.png)
Thanks!