You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.7 KiB

From: dalke at bioreason.com (Andrew Dalke)
Date: Sun, 25 Apr 1999 19:14:51 -0600
Subject: Handling backspace chars in a string...
References: <37239c48.594910176@news2.bga.com>
Message-ID: <3723BE0B.EFEC289A@bioreason.com>
Content-Length: 1466
X-UID: 148
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