Replace with backspace

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
usbobbie
Posts: 11
Joined: Wed Jan 05, 2005 4:11 pm

Replace with backspace

Post 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.
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post 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
Hope this was helpful.............good luck,
Bob
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

To insert a literal backspace use the replacement expression \x08.
usbobbie
Posts: 11
Joined: Wed Jan 05, 2005 4:11 pm

Post 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.
usbobbie
Posts: 11
Joined: Wed Jan 05, 2005 4:11 pm

Post 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?
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post 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)
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post 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.
Last edited by ben_josephs on Thu Jan 27, 2005 4:59 pm, edited 1 time in total.
ben_josephs
Posts: 2459
Joined: Sun Mar 02, 2003 9:22 pm

Post 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" ].
usbobbie
Posts: 11
Joined: Wed Jan 05, 2005 4:11 pm

Post 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
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post 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.
Hope this was helpful.............good luck,
Bob
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post 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.
Post Reply