Community
    • Login

    Replace text with incremented counter?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    pythonscriptregex
    5 Posts 2 Posters 5.9k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Guilherme SommerG Offline
      Guilherme Sommer
      last edited by

      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++?

      Scott SumnerS 1 Reply Last reply Reply Quote 1
      • Scott SumnerS Offline
        Scott Sumner @Guilherme Sommer
        last edited by

        @Guilherme-Sommer

        This cannot be done with normal regular-expression replacement in Notepad++.

        Scripting is the way to go. Have a look at the editor.rereplace function in the Pythonscript documentation. There is an example there that is really close to what you want to do.

        Guilherme SommerG 1 Reply Last reply Reply Quote 0
        • Guilherme SommerG Offline
          Guilherme Sommer @Scott Sumner
          last edited by

          @Scott-Sumner

          I had tried a similar code already
          But it’s not what I want

          What 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>

          Scott SumnerS 1 Reply Last reply Reply Quote 0
          • Scott SumnerS Offline
            Scott Sumner @Guilherme Sommer
            last edited by dail

            @Guilherme-Sommer

            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);
            
            Guilherme SommerG 1 Reply Last reply Reply Quote 2
            • Guilherme SommerG Offline
              Guilherme Sommer @Scott Sumner
              last edited by

              @Scott-Sumner

              It worked perfectly!! Thank you very much!

              1 Reply Last reply Reply Quote 2

              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
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors