Handling backspace chars in a string...

Andrew Dalke dalke at bioreason.com
Sun Apr 25 21:14:51 EDT 1999


bwizard at bga.com (Purple) said:
> I'm in the posistion of having to process strings with arbitrary
> numbers of backspace and newline characters in them. The backspaces
> actually get put in the string, so I have to handle removing the
> characters that are backspaced over.
> 
> [one implementation given]
>
> This just looked rather messy to me -- I was curious if anyone know
> a better way?

Here's one possibility.  It uses a regular expression substitution
to replace <any character> + <backspace> with the empty string.
(Note: don't use a raw string for the re; r".\b" will find a character
which is before a word break.)  When done, it removes all the
leading backspaces.


import re
char_backspace = re.compile(".\b")    # Don't use a raw string here
any_backspaces = re.compile("\b+")  # or here

def apply_backspaces(s):
  while 1:
    t = char_backspace.sub("", s)
    if len(s) == len(t):
      # remove any backspaces which may start a line
      return any_backspaces.sub("", t)
    s = t


>>> apply_backspaces("\bQ\b\bAndqt\b\brew Dalkt\br\be")
'Andrew Dalke'


You mentioned something about containing newlines.  By default, the
"." re pattern doesn't match a \n, so the above code acts like a
normal tty, and doesn't remove the \n if followed by a newline.  This
is likely the right thing.  That's also why I delete any backspace
because
  "this\n\bthat"

should be the same string as
  "this
that"

						Andrew Dalke
						dalke at acm.org




More information about the Python-list mailing list