Page 1 of 1

Replace with backspace

Posted: Thu Jan 27, 2005 3:27 pm
by usbobbie
How can I use regular expressions to replace the text [Backspace] with a real backspace? I can find it with \[Backspace\] and have tried to replace it with [\b] and \b with POSIX both on and off. Each time it simply replaces it literally with [\b] or \b.

Posted: Thu Jan 27, 2005 3:47 pm
by Bob Hansen
I think you may actually need to replace the preceeding character with nothing. Something like this:

Search for: .{1}\[backspace\]
Replace with: nothing

--------------------------------------------

Testing:
Starting with this:
abcdefgp[backspace]hijklm

Results with this:
abcdefghijklm

--------------------------------------------
Using Regular Expression with POSIX

This example will have problems if multiple backspaces like this:
abcdefwp[backspace][backspace]ghijklm

Posted: Thu Jan 27, 2005 4:02 pm
by ben_josephs
To insert a literal backspace use the replacement expression \x08.

Posted: Thu Jan 27, 2005 4:03 pm
by usbobbie
I do have multiples as you suggested. It fails on those since the cursor position is already past the previous character. Therefore, after the second iteration of the replace on [Backspace][Backspace][Backspace], it looks like this [Backspace since it selects [Backspace][Backspace].

It somehow needs to replace it with a backspace so that the cursor position does not bypass it. I also need it to do the real backspace so that it removes the previous character as well... as you were trying to do.

Posted: Thu Jan 27, 2005 4:15 pm
by usbobbie
ben, That replaces it with a block symbol but doesn't actually perform the backspace. Using Bob's suggestion on this then gets me the same results.

Is there a way to find any number of occurences of something, then grab the same number or prior characters, then replace them all with nothing?

Posted: Thu Jan 27, 2005 4:49 pm
by MudGuard
Find

Code: Select all

[^]]\[Backspace\]
Replace with nothing
Repeat until the replace operation gives an error "not found".

This transforms

xxxabc[Backspace][Backspace][Backspace]def

in the first step to

xxxab[Backspace][Backspace]def

in the second step to

xxxa[Backspace]def

and after the third step

xxxdef

As regexes can't count, I see no singlestep solution.

(as always, POSIX syntax is used)

Posted: Thu Jan 27, 2005 4:52 pm
by ben_josephs
The block symbol represents a "real backspace" (0x08), which is what you orginally asked for.

What you are looking for can't be done with a single application of a regular expression, as regular expression recognisers "can't count". But once you have replaced [backspace] with any single character of your choice (say # (where # doesn't occur elsewhere in the file)), you can repeatedly replace [^#]# by nothing.

Posted: Thu Jan 27, 2005 4:56 pm
by ben_josephs
The advantage of initially replacing [backspace] with a single character is that the subsequent repeated replacement will then correctly handle a backspace following a "genuine" ].

Posted: Thu Jan 27, 2005 5:54 pm
by usbobbie
Ben, I realize that the block symbol was the unprintable backspace character, but I was hoping it could actually do the backspace. MudGuard's (and your) suggestion to use [^...] instead of .{1} works. I simply keep hitting "Replace All" until it can't find any more. Thanks to all :D

Posted: Fri Jan 28, 2005 4:58 am
by Bob Hansen
You could also handle multiple backspaces by making multiple passes with a modifed version of my first suggestion with one for three backspaces, then two backspaces, and then one backspace.

Untested:
Search for: .{3}(\[backspace\]){3}
Replace with: nothing

Search for: .{2}(\[backspace\]){2}
Replace with: nothing

Search for: .{1}\[backspace\]
Replace with: nothing

If this works, then make a macro to run them in sequence.

Posted: Fri Jan 28, 2005 10:31 am
by MudGuard
Bob, in your case, the maximum number of consecutive backspaces is limited

e.g. if you only use 3, but there are seven, this:
987654321[backspace][backspace][backspace][backspace][backspa0ce][backspace][backspace]
produces
987654[backspa
when doing the 3-backspaces deleting

More general: if the longest sequence you program for is n but there is a sequence of 2n+1, your method fails.