Python Forum
Regex: Remove all match plus one char before all - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Regex: Remove all match plus one char before all (/thread-2137.html)

Pages: 1 2 3 4


RE: Regex: Remove all match plus one char before all - wavic - Feb-24-2017

Alright, If str.replace() all '|BS|' with '\b' and print the result it prints what is wanted. How to "evaluate" 'it \x08\x08\x08this is one\x08\x08\x08an example' to 'this is an example'? There is no need for loops.


RE: Regex: Remove all match plus one char before all - zivoni - Feb-24-2017

I would guess that this evaluation is done on terminal level, so it may be not so easy to "reuse".


RE: Regex: Remove all match plus one char before all - wavic - Feb-25-2017

So I've played a lot but couldn't get it to work. This subprocess module squeezed my brain  Shocked

In [1]: text = subprocess.Popen(['echo', '-e', 'it \x08\x08\x08this is one\x08\
    ...: x08\x08an example'])

this is an example

In [2]: text = subprocess.Popen(['echo', '-e', 'it \x08\x08\x08this is one\x08\
    ...: x08\x08an example'], stdout=subprocess.PIPE)

In [3]: text

Out[3]: <subprocess.Popen at 0x7f5a2f791048>

In [4]: text.communicate()[0]

Out[4]: b'it \x08\x08\x08this is one\x08\x08\x08an example\n'
How does this module work  Wall


RE: Regex: Remove all match plus one char before all - zivoni - Feb-25-2017

Big Grin  I tried exactly same thing yesterday. But it didnt work at all, neither it worked in terminal, so I think that parsing <BS> is done at the end in the terminal ...

Output:
jacq /tmp> /bin/echo -e "123\b45" 1245 jacq /tmp> /bin/echo -e "123\b45" | hexdump -C 00000000  31 32 33 08 34 35 0a                              |123.45.|      # <-- still there



RE: Regex: Remove all match plus one char before all - nilamo - Mar-27-2017

I'm late to the party, and mine looks a lot like a few of the others, but here's my entry:

orig = 'it |BS||BS||BS|this is one|BS||BS||BS|an example'

import re
regex = re.compile(r".?\|BS\|")

text = orig
replacements = 1
while replacements:
   text, replacements = regex.subn("", text, 1)
print(text) # this is an example