Replace with backspace
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
Replace with backspace
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.
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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
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
Bob
-
- Posts: 2461
- Joined: Sun Mar 02, 2003 9:22 pm
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.
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.
Find 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)
Code: Select all
[^]]\[Backspace\]
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)
-
- Posts: 2461
- Joined: Sun Mar 02, 2003 9:22 pm
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.
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.
-
- Posts: 2461
- Joined: Sun Mar 02, 2003 9:22 pm
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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.
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
Bob
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.
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.