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