• Login
Community
  • Login

Replace text with incremented counter?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
pythonscriptregex
5 Posts 2 Posters 5.1k Views
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.
  • G
    Guilherme Sommer
    last edited by Feb 23, 2018, 2:34 PM

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

    S 1 Reply Last reply Feb 23, 2018, 2:40 PM Reply Quote 1
    • S
      Scott Sumner @Guilherme Sommer
      last edited by Feb 23, 2018, 2:40 PM

      @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.

      G 1 Reply Last reply Feb 23, 2018, 3:11 PM Reply Quote 0
      • G
        Guilherme Sommer @Scott Sumner
        last edited by Feb 23, 2018, 3:11 PM

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

        S 1 Reply Last reply Feb 23, 2018, 3:34 PM Reply Quote 0
        • S
          Scott Sumner @Guilherme Sommer
          last edited by dail Feb 23, 2018, 4:20 PM Feb 23, 2018, 3:34 PM

          @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);
          
          G 1 Reply Last reply Feb 23, 2018, 6:46 PM Reply Quote 2
          • G
            Guilherme Sommer @Scott Sumner
            last edited by Feb 23, 2018, 6:46 PM

            @Scott-Sumner

            It worked perfectly!! Thank you very much!

            1 Reply Last reply Reply Quote 2
            5 out of 5
            • First post
              5/5
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors