0

Replace text incrementally on a line by line basis (restarting counting with each line).

I have tried various solutions like the Notepad++ Column replacement, TextPad incremental replacement and the Python script as listed here Notepad++ incrementally replace but none worked they way I wanted (they would not replace on a full line nor restart from each line).

I got as far as replacing with the find text, but do not know how to increment it.

FIND: (\[2s\])
REPLACE: <br /><b class="num">\1</b>

So I want this:

test text various [2s] tes text various  [2s] tes text various [2s] to replace w abc-mine1.txt , [2s] to replace w abc-mine1.txt , [2s] to replace w abc-mine1.txt , [2s] to replace w
test text various [2s] tes text various  [2s] tes text various [2s]
test text various [2s]

To become this

test text various <br /><b class="num">1</b> tes text various  <br /><b class="num">2</b> tes text various <br /><b class="num">3</b> to replace w abc-mine1.txt , <br /><b class="num">4</b>] to replace w abc-mine1.txt , <br /><b class="num">5</b> to replace w abc-mine1.txt , <br /><b class="num">6</b> to replace w
test text various <br /><b class="num">1</b> tes text various  <br /><b class="num">2</b> tes text various <br /><b class="num">3</b>
test text various <br /><b class="num">1</b>
Crick3t
  • 1,011
  • 1
  • 14
  • 23
greektranslator
  • 499
  • 1
  • 6
  • 19
  • See [Notepad++ Find/Replace number with Increment Value](https://stackoverflow.com/questions/37300462/notepad-find-replace-number-with-increment-value/37300757#37300757) – Wiktor Stribiżew Jan 09 '19 at 14:04
  • Could not adapt it to work in my case. What I used had no effect: ```def increment_after_openparen(match): return "(2s".format(str(int(match.group(1))+1)) editor.rereplace(r'\((\d+)', increment_after_openparen)``` And the increment in the cited example is applied on a column basis, not on a line basis as I need. – greektranslator Jan 09 '19 at 14:35

1 Answers1

1

I think you cannot do it with normal regex find and replace.

But if you can install the Python Script Plugin then the following code should do the trick:

repstr = "[2s]"
lcount = editor.getLineCount()  
newstr = ""
i = 0
while i < lcount:
    actline = editor.getLine(i)
    counter = 1
    while actline.find(repstr) > 0:
        actline = actline.replace(repstr, "<br /><b class=\"num\">"+str(counter)+"</b>",1)
        counter += 1
    newstr = newstr + actline
    i += 1

editor.clearAll();
editor.appendText(newstr);
Crick3t
  • 1,011
  • 1
  • 14
  • 23
  • Yep, that does it. I found a low tech solution before that (replace all variable instances with tab, paste to Excel, populate inbetween columns with incremental number, merge columns but will definitely use this script next time :) – greektranslator Jan 10 '19 at 19:20