Find a formatted number and replace with the line numer

General questions about using TextPad

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

Post Reply
User avatar
jmparatte
Posts: 4
Joined: Fri Nov 25, 2011 11:07 am
Location: Switzerland

Find a formatted number and replace with the line numer

Post by jmparatte »

My wish: Find a pattern '(ddd)' and replace ddd with the line number.
Example with ActionsScripot3:
...
trace('signature','(123)',...);
...
The idea is to replace '(123)' by the line number, because after editing text, the line number has changed.

What to find: [(][0-9]+[)]
Replace: (\i)
But \i is not the correct line number, it's the count of replacements.

With PHP, the problem doesn't exist because __LINE__ constant exist.
Last edited by jmparatte on Sat Nov 26, 2011 7:40 am, edited 1 time in total.
ak47wong
Posts: 703
Joined: Tue Aug 12, 2003 9:37 am
Location: Sydney, Australia

Post by ak47wong »

It can't be done practically because there's no __LINE__ constant or other equivalent expression that returns the line number. If you absolutely had to fudge it, you could do it the following way, in three Replace operations.

Before starting, click Configure > Preferences > Editor and enable POSIX regular expression syntax. In the following examples, I've used the string "~~" on the assumption that it does not otherwise occur in your file. If it does, substitute another string.

Step 1 is to add the line number at the start of each line:

Find what: ^
Replace with: \i~~

Step 2 is to find the pattern "(ddd)" and replace "ddd" with the line number at the start of the line:

Find what: ([0-9]*)(~~.*\()[0-9]+(\).*)
Replace with: \1\2\1\3

Step 3 is to remove the line numbers at the start of each line:

Find what: [0-9]*~~
Replace with: (nothing)

Note however that if "(ddd)" occurs more than once per line this procedure will only replace the last occurrence.
User avatar
jmparatte
Posts: 4
Joined: Fri Nov 25, 2011 11:07 am
Location: Switzerland

Post by jmparatte »

Thank you very match for the reply.
I never take care of option POSIX before :D
User avatar
kengrubb
Posts: 324
Joined: Thu Dec 11, 2003 5:23 pm
Location: Olympia, WA, USA

Post by kengrubb »

A tweak to ak47wong's excellent suggestion.

In Step 2

Find what: ^([0-9]*)(~~.*\()[0-9]+(\).*)

In Step 3

Find what: ^[0-9]*~~

Then it should work even if there are double tildes in the file.
(2[Bb]|[^2].|.[^Bb])

That is the question.
Post Reply