Replace text with incremented counter?
-
Newbie here - please be gentle! :)
I’m sure there must already be a way to do this, but I haven’t yet figured it out. I’m trying to find a way to replace text with an incremented counter. For example, if I have a file containing this:
= = = = = = = = = =
<TAG>9999</TAG>
<TAGHERE>
<OTHERTAG>Lorem ipsum dolor sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</OTHERTAG>
</TAGHERE>
<TAG>9999</TAG>
<TAGHERE>
<OTHERTAG>Lorem ipsum dolor sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</OTHERTAG>
</TAGHERE>
<TAG>9999</TAG>
= = = = = = = = = =I’d like to replace the first occurrence of “9999” with “0”, the second occurrence of “9999” with “1”, etc., resulting in:
= = = = = = = = = =
<TAG>0</TAG>
<TAGHERE>
<OTHERTAG>Lorem ipsum dolor sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</OTHERTAG>
</TAGHERE>
<TAG>1</TAG>
<TAGHERE>
<OTHERTAG>Lorem ipsum dolor sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</OTHERTAG>
</TAGHERE>
<TAG>2</TAG>
= = = = = = = = = =I don’t see any way to do this through regular expressions. I’m guessing it might be possible with Python Script plugin, but I’m apparently too stupid to figure out how. Is there a way to do this with Notepad++?
-
This cannot be done with normal regular-expression replacement in Notepad++.
Scripting is the way to go. Have a look at the
editor.rereplacefunction in the Pythonscript documentation. There is an example there that is really close to what you want to do. -
I had tried a similar code already
But it’s not what I wantWhat I did:
def calculate(match):
return ‘<TAG>%s’ % (str(int(match.group(1))+1))
editor.rereplace(‘<TAG>([0-9]+)’, calculate)But this increments the number inside <TAG>
Meaning this
<TAG>120<TAG>
<TAG>300<TAG>
<TAG>552<TAG>Become this
<TAG>121<TAG>
<TAG>301<TAG>
<TAG>553<TAG>And what I want is this
<TAG>0<TAG>
<TAG>1<TAG>
<TAG>2<TAG> -
Maybe try something more like this?:
count = -1 def calculate(m): global count count += 1 return '<TAG>' + str(count) + '<TAG>' editor.rereplace('<TAG>([0-9]+)<TAG>', calculate); -
It worked perfectly!! Thank you very much!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login