Insert character going backwards?

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
telephone
Posts: 15
Joined: Thu Jun 28, 2007 2:33 pm

Insert character going backwards?

Post by telephone »

How would I insert a \ character into the numbers below? I would need it to start at the end of the line and go back every two characters to insert
the \, until the proceeding character is the only character left or two accompanied with the beginning of the line, then it should stop and shouldn't insert the \ either. Any help would be appreciated.

Thanks.

Example:

167
298
6423
6578
99771
99854
100286
100288


Desired output:

1\67
2\98
64\23
65\78
9\97\71
9\98\54
10\02\86
10\02\88

Thanks
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Here's one way to do it:
Find what: ([0-9])([0-9][0-9])(\\|$)
Replace with: \1\\\2\3

[X] Regular expression

Replace All
Replace All repeatedly until it beeps.

This assumes you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Last edited by ben_josephs on Sun Feb 10, 2008 11:49 pm, edited 1 time in total.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Here's another:
Find what: ([0-9])(([0-9][0-9])+)$
Replace with: \1\\\2

[X] Regular expression

Replace All
Replace All repeatedly until it beeps.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

TextPad RegEx does not have lookback ability, but we may still be able to do something.

Do you know how long the longest string might be.
Is there anything else on the lines?
If grouped on the same line do they have a common delimiter like a space char? That knowledge may provide something easier than the following approach......

You could do multiple passes with something like the following UNTESTED replacements:

Pass 1:
Replace: ([0-9]{2})\n
With: \\\1\n

Then Pass 2,3,4, etc on selected lines, until done:
Replace: ([0-9]{2}\\)(.+)
With: \\\1\2
===========================================
Use the following settings:
-----------------------------------------
[X] Regular expression
Replace All
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------

=======================
Edited: My solution was submitted while ben_josephs was submitting his. His is more correct if the lengths are always the limited lengths that you submitted. My approach was for unlimited\unknown lengths.
Hope this was helpful.............good luck,
Bob
telephone
Posts: 15
Joined: Thu Jun 28, 2007 2:33 pm

Post by telephone »

Wow, thanks you two. Works great.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Bob Hansen wrote:[ben_josephs's approach] is more correct if the lengths are always the limited lengths that you submitted. My approach was for unlimited\unknown lengths.
Both of my suggestions work on lines of any length. Your suggestion doesn't know when it's finished, so it doesn't beep, and you end up repeatedly inserting backslashes on the left.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Hi ben_josephs.

I am constantly learning from you, and appreciate your patience, and admire your skills.

But I seem to be doing something wrong with the solutions I am trying from your samples above.

This is the sample I am using to run against, unknown lengths.......
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012
1234567890123
12345678901234
123456789012345
1234567890123456
12345678901234567
123456789012345678
1234567890123456789
12345678901234567890

And here is the result I get for solution 1:
1
12
1\23
12\34
123\45
1234\56
12345\67
123456\78
1234567\89
12345678\90
123456789\01
1234567890\12
12345678901\23
123456789012\34
1234567890123\45
12345678901234\56
123456789012345\67
1234567890123456\78
12345678901234567\89
123456789012345678\90

And here is the result I get for solution 2:
1
12
1\23
12\34
1\2345
12\3456
1\234567
12\345678
1\23456789
12\34567890
1\2345678901
12\3456789012
1\234567890123
12\345678901234
1\23456789012345
12\34567890123456
1\2345678901234567
12\3456789012345678
1\234567890123456789
12\345678901234567890

What error have I made?

Re my samples, they were untested, and I just did try them out. The second section to be run repeatedly was not intended to run automatically, but it does require manually repeating the Replace process by selecting a new group with each run, and that turns out be starting at every odd numbered line, which is really impractical. In the sample above, I had to select lines 3-20, then 5-20, then 7-20, etc. Not acceptable. The results were also pretty close, but not correct. Here is my result:
1
\12
1\23
12\34
1\23\45
12\34\56
1\23\45\67
12\34\56\78
1\23\45\67\89
12\34\56\78\90
1\23\45\67\89\01
12\34\56\78\90\12
1\23\45\67\89\01\23
12\34\56\78\90\12\34
1\23\45\67\89\01\23\45
12\34\56\78\90\12\34\56
1\23\45\67\89\01\23\45\67
12\34\56\78\90\12\34\56\78
1\23\45\67\89\01\23\45\67\89
12345678901234567890
==========================
So, rather than spending more time on this, I think your examples are probably going to be correct, but I need to understand why they are not working for me on the 20 line sample I am using.

Thanks again for your help.
Hope this was helpful.............good luck,
Bob
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Bob Hansen wrote:What error have I made?
ben_josephs wrote:Replace All repeatedly until it beeps.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Gotta teach this guy how to read! :oops:

Beautiful. Works great. Thanks again......
Hope this was helpful.............good luck,
Bob
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Sorry. Reading lessons you've got to pay for.

:-)
Post Reply