Entry
Handling backspace chars in a string...
Jul 5th, 2000 10:00
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 157, Andrew Dalke
"""
Packages: basic_datatypes.strings
"""
"""
> 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
print 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"
"""