Page 1 of 1

Find a formatted number and replace with the line numer

Posted: Fri Nov 25, 2011 11:16 am
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.

Posted: Fri Nov 25, 2011 11:17 pm
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.

Posted: Sat Nov 26, 2011 7:46 am
by jmparatte
Thank you very match for the reply.
I never take care of option POSIX before :D

Posted: Wed Nov 30, 2011 9:28 pm
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.