Community
    • Login

    Replacing number with another Incrementing #

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    18 Posts 5 Posters 11.9k 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.
    • astrosofistaA
      astrosofista @raizkie19
      last edited by

      @raizkie19 said in Replacing number with another Incrementing #:

      But the number line will be long… From 40001 to 70000… :(

      Hi @raizkie19, @Ekopalypse, @Alan-Kilborn and All

      As @Ekopalypse said, regex can’t do math, but there are workarounds to simulate some common operations. So, as an alternative to @Alan-Kilborn’s script and in case you are not allowed to install the Python plugin, let me suggest you the following method, which is based on previous posts.

      Please try the following:

      Open a new tab in Notepad++ (Ctrl + N)
      Type in a space and then press the Enter key
      Open the Replace dialog (Ctrl + H)
      Set the following fields as follows (Copy/Paste):

      Find what: \R
      Replace with: \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n

      Only select the Wrap Around option and the Regular expression search mode
      Click 5 times the Replace All button in order to get 32768 (= 2^15) lines with a blank space
      Close the Replace dialog

      Press Ctrl + Home to go to very beginning of the file.

      Now, open the Column editor (Edit -> Column Editor…, or Alt + C)

      Check the Number to Insert option
      Type 100 in the Initial number field
      Type 1 in the Increase by field
      Leave the options Repeat and Leading zeros empty
      The format is decimal, by default
      Click on the OK button

      Place the caret at the beginning of the file and then press End to put it after the blank space that follows 100.

      Again, open the Column editor (Edit -> Column Editor…, or Alt + C)

      Check the Number to Insert option
      Type 40001 in the Initial number field
      Type 1 in the Increase by field
      Leave the options Repeat and Leading zeros empty
      The format is decimal, by default
      Click on the OK button

      The replacement list is done. You should have got a list as to this one:

      100   40001
      101   40002
      102   40003
      103   40004
      104   40005
      [...]
      32864 72765
      32865 72766
      32866 72767
      32867 72768
      32868 72769
      

      Copy the whole list (Ctrl + A)

      Open a copy of the file to be changed.
      Go to the last line.
      Press Enter, then type === (three equal signs) and press Enter again.
      Paste the list to get the following:

      30000#
      A tuber that can be fried, baked, boiled mashed, even eaten.
      ===
      100   40001
      101   40002
      102   40003
      [...]
      

      Return to the first line of the file (Ctrl + Home)
      Open the Replace dialog (Ctrl + H)
      Deselect all options except the Regular expression search mode
      Set the following fields as follows (Copy/Paste):

      Search: (?s)^(\d+)(?=#\R.*?===.*?\1 +(\d+))
      Replace: ?1$2

      Now, the last action:
      Click on the Replace All button
      Close de Replace dialog.

      That’s all. All the numbers should have been changed.

      Best Regards.

      Alan KilbornA 1 Reply Last reply Reply Quote 2
      • Alan KilbornA
        Alan Kilborn @astrosofista
        last edited by

        @astrosofista

        Now THAT is a serious workaround, for the truly desperate. :-)

        astrosofistaA 1 Reply Last reply Reply Quote 2
        • astrosofistaA
          astrosofista @Alan Kilborn
          last edited by

          @Alan-Kilborn said in Replacing number with another Incrementing #:

          Now THAT is a serious workaround, for the truly desperate. :-)

          Yep, looks as a quite hard task, but actually isn’t. It would be easier to deliver if the macro feature could record Column Editor outputs.

          Later will try your nice Python script.

          1 Reply Last reply Reply Quote 0
          • astrosofistaA
            astrosofista @Alan Kilborn
            last edited by

            @Alan-Kilborn said in Replacing number with another Incrementing #:

            Specify ^\d{3}(?=#) in the input box that appears. Press OK.

            Tested and worked fine on sample text :)

            However, I found a potential failure. OP told us that he wanted to replace 30,000 numbers, so if these grow by one, as the sample text suggests, then the regex you provided will fail to match four or five digit numbers.

            If this is the case, then as you know, expressions like ^\d{3,5}(?=#) or ^\d+(?=#) will match all the numbers.

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @astrosofista
              last edited by

              @astrosofista

              I found a potential failure.

              True, but also kind of obvious. :-)
              Plus, I should have used “e.g.” on that part of it, like I did in 2 other places.

              But yes, the OP may not be versed in regex, and may not know how to specify a solution that covers all his cases, so thanks for the pick-me-up on that.

              For me, it was more about publishing the script, which I had sitting unfinished in my N++ tabs ever since @Ekopalypse published his original script (which only selected multiple occurrences of static text).

              astrosofistaA 1 Reply Last reply Reply Quote 2
              • astrosofistaA
                astrosofista @Alan Kilborn
                last edited by

                @Alan-Kilborn said in Replacing number with another Incrementing #:

                For me, it was more about publishing the script

                Rest assured your script is a nice and useful improvement, since it allows users to make more complex selections with greater flexibility.

                Thank you for sharing it with us :)

                1 Reply Last reply Reply Quote 1
                • astrosofistaA
                  astrosofista @raizkie19
                  last edited by

                  Hi @raizkie19, @Ekopalypse, @Alan-Kilborn, All

                  I have just realised that, given the regularity of the problem in question, it can also be stated as a mathematical operation, a simple addition. This approach, which perhaps was the one that @ekopalypse had in mind in his post above, gives rise to a third alternative, simpler and more direct than the two posted so far, since although it still requires the Python plugin and a regex, it doesn’t need any list created with the Column Editor.

                  The following script is a very minor adaptation of a code posted by @ekopalypse to solve a similar problem, so all credits belongs to him:

                  from Npp import editor
                  
                  def add_raizkie_number(m):
                      return int(m.group(0)) + 39901
                  
                  editor.rereplace('\d+(?=#)', add_raizkie_number)
                  

                  So, @raizkie19 hope you can test it and see if it deliver the expected outcome. It worked fin here.

                  Have fun!

                  Alan KilbornA 1 Reply Last reply Reply Quote 2
                  • Alan KilbornA
                    Alan Kilborn @astrosofista
                    last edited by

                    @astrosofista said in Replacing number with another Incrementing #:

                    very minor adaptation of a code posted by @ekopalypse to solve a similar problem
                    so all credits belongs to him

                    Actually, I think all of the credit goes back to the Pythonscript documentation!

                    931caaf5-cd94-4694-a2bf-aee81705980a-image.png

                    Note, though: number should be int in the documentation!

                    EkopalypseE PeterJonesP astrosofistaA 3 Replies Last reply Reply Quote 3
                    • EkopalypseE
                      Ekopalypse @Alan Kilborn
                      last edited by

                      @Alan-Kilborn @astrosofista

                      More specifically, I have modified the result of calling help(editor.rereplace) in the PythonScript console.

                      >>> help(editor.rereplace)
                      Help on method rereplace:
                      
                      rereplace(...) method of Npp.Editor instance
                          rereplace( (Editor)arg1, (object)searchRegex, (object)replace) -> None :
                              Regular expression search and replace. Replaces 'searchRegex' with 'replace'.  ^ and $ by default match the starts and end of the document.  Use additional flags (re.MULTILINE) to treat ^ and $ per line.
                              The 'replace' parameter can be a python function, that recieves an object similar to a re.Match object.
                              So you can have a function like
                                 def myIncrement(m):
                                     return int(m.group(1)) + 1
                              
                              And call rereplace('([0-9]+)', myIncrement) and it will increment all the integers.
                      1 Reply Last reply Reply Quote 2
                      • PeterJonesP
                        PeterJones @Alan Kilborn
                        last edited by

                        @Alan-Kilborn said in Replacing number with another Incrementing #:

                        Note, though: number should be int in the documentation!

                        That documentation bug was fixed in v1.5.3 in February.
                        v1.5.4 has been released since, with the fix to the getLanguageDesc() historical bug which we finally reported in #146.

                        1 Reply Last reply Reply Quote 2
                        • astrosofistaA
                          astrosofista @Alan Kilborn
                          last edited by

                          @Alan-Kilborn

                          May it be, mine was just a disclaimer, as I know almost nothing about Python :)

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