1

I need to get the following string as if it were printed using print()

str = '1234\b\b\b\b\r5678\n'

I want to apply the backspace characters ('\b','\r' and etc), for an output (str2) line of the form:

str2 = '5678\n'

And another example:

str = '1234\b\r5678\nqwert\b\b\b\b\b\b\b\b\r1234'

Result:

str = '5678\n1234'

3 Answers3

1

This will take care of at least \b and \r but it is not a generic solution for any control sequence that you could encounter

import re

def convert_to_term(str):
    """
    coverts \b (\x08) and \r as a printout to terminal would represent it
    """
    # \b backspace
    str = re.sub(r'.\x08', '', str)
    # \r carriage return
    str = re.sub(r'(^|\n)[^\n]+\r', r'\1', str)
    return str

in_str = '1234 \b \b \b \b \r5678 \n'
out_str = convert_to_term(in_str)
print ("\"" + out_str + "\"")
print (len(out_str))

in_str = '1234\b\r5678\nqwert\b\b\b\b\b\b\b\b\r1234'
out_str = convert_to_term(in_str)
print ("\"" + out_str + "\"")
print (len(out_str))

gives:

"5678 
"
6
"5678
1234"
9

Lutz
  • 612
  • 3
  • 8
  • Unfortunately this does not work for the case: given_string='1234\b\r5678\nqwert\b\b\b\b\b\b\b\b\r1234' The result should be: new_string='5678\n1234' – Паша Кашин Sep 24 '19 at 10:52
  • for me it works (after changing convert_to_term(str) to convert_to_term(in_str)) I get a 9 character string that looks just like 5678\n1234 – Lutz Sep 24 '19 at 10:58
-1

In python

 import re
 re.sub(r"[a-zA-z]","",re.sub(r"[^\w\n]","", '1234\b\r5678\nqwert\b\b\b\b\b\b\b\b\r1234'))

In javascript

"1234\b\r5678\nqwert\b\b\b\b\b\b\b\b\r1234".replace(/[^\w\n]/gi,"").replace(/[a-z]/gi,"")
AmitNayek
  • 158
  • 1
  • 9
-2
def apply_controls(string):
    pointer = 0
    output = []
    line_pointer = 0

    for character in string:
        #handles the alphanumeric characters
        if character.isalnum():
            output[pointer:pointer+1] = character
            pointer += 1
        #only does backspace if there are any characters on current line
        elif character == "\b":
            if pointer > line_pointer:
                output[pointer:pointer+1] = ''
                pointer -= 1
        #retains the new line and updates the line_pointer
        elif character == "\n":
            output[pointer:pointer+1] = character
            pointer += 1
            line_pointer = pointer
        #deletes all characters on the given line
        elif character == "\r":
            output[line_pointer:pointer+1] = ''
            pointer = line_pointer
        else:
            print("Unknown character: "+character)

    return "".join(output)

This does give the output you desire.

  • Will that code apply the backspace characters, or just remove them? Also, the regex as written will remove spaces, like the newline that's supposed to stay in the end. – Haem Sep 24 '19 at 09:01
  • if input is `"abcc\bdef"` your current code will output `"def"` instead of the expected `"abcdef"` – Cid Sep 24 '19 at 09:13
  • Unfortunately this does not work for the case: given_string='1234\b\r5678\nqwert\b\b\b\b\b\b\b\b\r1234' The result should be: new_string='5678\n1234' – Паша Кашин Sep 24 '19 at 10:00
  • @ПашаКашин: Try the new function and let me know if it works as desired. – makeshift-programmer Sep 24 '19 at 12:24