Replacement of text using Notepad ++

notepad

I'd like to be able to replace a certain string text in a .txt file that is open in Notepad ++ but using more than the simple text replacement. I need a query to search for a string that starts with:

# Model Data Entry xxx

and then to replace anything on the same line with:

# Model Data Entry yyy

whereby xxx is any random number and whereby yyy would be starting from 0 and ascending in numerical order replacing the text at the first instance in the file going up a value of 1 for each of the entries that follow. Example:

Original text:

# Model Data Entry 69
# Model Data Entry 119
# Model Data Entry 3
# Model Data Entry 71
# Model Data Entry 45

processed text:

# Model Data Entry 0
# Model Data Entry 1
# Model Data Entry 2
# Model Data Entry 3
# Model Data Entry 4...

It should be noted that there will be several lines of other text in between all the instances of # Model Data Entry

Best Answer

You can run a python script within the PythonScript plugin.

If it is not yet installed, follow this guide

Create a script (Plugins >> PythonScript >> New Script)

Copy this code and save the file (for example calculate.py):

import re

counter = -1
def calculate(match):
    global counter
    counter += 1
    return ' ' + str(counter)

editor.rereplace('(?<=# Model Data Entry)\h*\d*', calculate)
  • Open the file you want to change
  • Run the script (Plugins >> PythonScript >> Scripts >> calculate)
  • Done

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Related Question