Python Forum
Regex: Remove all match plus one char before all
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex: Remove all match plus one char before all
#31
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#32
I would guess that this evaluation is done on terminal level, so it may be not so easy to "reuse".
Reply
#33
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#34
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
Reply
#35
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Facing issue in python regex newline match Shr 6 1,148 Oct-25-2023, 09:42 AM
Last Post: Shr
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Failing regex, space before and after the "match" tester_V 6 1,118 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Regex pattern match WJSwan 2 1,193 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Match substring using regex Pavel_47 6 1,370 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Match key-value json,Regex saam 5 5,301 Dec-07-2021, 03:06 PM
Last Post: saam
  How to replace on char with another in a string? korenron 3 2,294 Dec-03-2020, 07:37 AM
Last Post: korenron
  How to remove char from string?? ridgerunnersjw 2 2,486 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  regex.findall that won't match anything xiaobai97 1 1,974 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE
  Creating new list based on exact regex match in original list interjectdirector 1 2,254 Mar-08-2020, 09:30 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020