Community
    • Login

    re-numbering phrase+number in the middle of sentence

    Scheduled Pinned Locked Moved General Discussion
    10 Posts 3 Posters 802 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.
    • B MRB
      B MR
      last edited by

      hi, this is part of a file which i need to incremental by 1 inside the phrase “int[] arr1”,
      how can i do it?

          int[] arr1 = {6, 7, 8}; 
          listArr.add(arr1); 
          int[] arr1 = {4, 5, 6}; 
          listArr.add(arr2); 
          int[] arr1 = {2, 3, 4};
      
      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @B MR
        last edited by

        @B-MR ,

        With just Notepad++ and nothing else, you cannot. If you are willing to use a plugin, it’s easy.

        You can install the PythonScript plugin, and use the example add_1 that’s in the documentation; you’d have to change the search regular expression in the script to suit your needs

        You can search this forum for examples of add_1 and related pythonscript replacements

        Based on my interpretation of what you said,

        # encoding=utf-8
        """this uses the "add_1" style search and replace in Notepad++
        
        INSTRUCTIONS:
        1. Install PythonScript
           * Plugins > Plugins Admin
           * Click ☑ PythonScript checkbox until checked
           * click INSTALL
           * restart Notepad++ as necessary
        2. Create new PythonScript script
           * Plugins > PythonScript > New Script
           * Give it a reasonable name, like add_1.py
           * Paste in the code from this black box
           * Save
        3. Switch to the document where you want to search/replace
        4. Plugins > PythonScript > Scripts > add_1
        
        To assign a keyboard shortcut:
        1. Plugins > PythonScript > Configuration
        2. In the User Scripts, click on add_1.py
        3. Click the left ADD button (above Menu Items)
            * This adds "add_1" to the main Plugins > PythonScript menu, rather
              than requiring you to dig down into the Scripts submenu
        4. Exit Notepad++ and restart
            * This is required for ShortcutMapper to be able to see the new entry
              in the PythonScript menu
        5. Settings > ShortcutMapper > Plugin Commands
        6. Filter by add_1
        7. Click on add_1, then click the MODIFY button and
            set the keyboard shortcut in the dialog
        
        You can change the replacement values, or the number of replacements,
        then save, and it will still run.
        
        PythonScript has documentation in its Context-Sensitive Help
        """
        from Npp import *
        
        counter = 1
        
        def add_1(m):
            global counter
            repl = str(counter)
            counter = counter + 1
            return repl
        
        def disp_1(m):
            console.write("found {}\n".format(m.group()))
        
        editor.rereplace(r'(?<=int\[\] arr)\d+', add_1)
        

        which turned your example data into

            int[] arr1 = {6, 7, 8}; 
            listArr.add(arr1); 
            int[] arr2 = {4, 5, 6}; 
            listArr.add(arr2); 
            int[] arr3 = {2, 3, 4};
        
        Alan KilbornA 1 Reply Last reply Reply Quote 2
        • B MRB
          B MR
          last edited by

          i do not know what and how to change the code to my needs
          thank you any way

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

            @PeterJones

            I think the forum again consumed the backslash in front of [ and ] in your code. I think this should be the correct line in the code:

            d7504737-67ef-45f0-8e25-79271fe780c6-image.png

            @B-MR said:

            i do not know what and how to change the code to my needs

            Aside from the forum posting error I mentioned, Peter gave you everything you’d need for what you asked for – did you even try to follow the step-by-step-spoon-feed-me instructions?

            1 Reply Last reply Reply Quote 1
            • B MRB
              B MR
              last edited by

              @PeterJones said in re-numbering phrase+number in the middle of sentence:

              rere

              sorry, i did not, i don’t have any knowledge of code,
              but maybe i’ll try later

              thank you alan

              1 Reply Last reply Reply Quote 0
              • B MRB
                B MR
                last edited by

                tried it
                step by step
                and it did nothing

                this is what copied:
                from Npp import *

                counter = 1

                def add_1(m):
                global counter
                repl = str(counter)
                counter = counter + 1
                return repl

                def disp_1(m):
                console.write(“found {}\n”.format(m.group()))

                editor.rereplace(r’(?<=int[] arr) \d+', add_1)

                Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @B MR
                  last edited by

                  @PeterJones

                  Just a note to say it worked perfectly when I tried it, of course with the slight tweak I made to it because of the forum posting error (as detailed above).

                  1 Reply Last reply Reply Quote 0
                  • PeterJonesP
                    PeterJones @B MR
                    last edited by

                    @B-MR ,

                    fecf8835-adaf-40b6-9049-675a30410745-image.png

                    If that’s literally and exactly what’s inside your Notepad++ window when editing the script, it is definitely not going to work, as that’s not valid python code.

                    As @Alan-Kilborn said, the forum got rid of two backslashes, as he showed in a screenshot earlier. So you need to make sure you include those backslashes

                    e442cf57-f06d-4b61-96dd-277b73c04eb6-image.png
                    (please note: mine starts at line 37 because I also have all the other lines from the black box in the saved *.py file – those are comments, and can be saved as part of the script for future reference)

                    Let me try to re-embed the code in the forum for easy copy/paste:

                    # encoding=utf-8
                    """this uses the "add_1" style search and replace in Notepad++
                    
                    INSTRUCTIONS:
                    1. Install PythonScript
                       * Plugins > Plugins Admin
                       * Click ☑ PythonScript checkbox until checked
                       * click INSTALL
                       * restart Notepad++ as necessary
                    2. Create new PythonScript script
                       * Plugins > PythonScript > New Script
                       * Give it a reasonable name, like add_1.py
                       * Paste in the code from this black box
                       * Save
                    3. Switch to the document where you want to search/replace
                    4. Plugins > PythonScript > Scripts > add_1
                    
                    To assign a keyboard shortcut:
                    1. Plugins > PythonScript > Configuration
                    2. In the User Scripts, click on add_1.py
                    3. Click the left ADD button (above Menu Items)
                        * This adds "add_1" to the main Plugins > PythonScript menu, rather
                          than requiring you to dig down into the Scripts submenu
                    4. Exit Notepad++ and restart
                        * This is required for ShortcutMapper to be able to see the new entry
                          in the PythonScript menu
                    5. Settings > ShortcutMapper > Plugin Commands
                    6. Filter by add_1
                    7. Click on add_1, then click the MODIFY button and
                        set the keyboard shortcut in the dialog
                    
                    You can change the replacement values, or the number of replacements,
                    then save, and it will still run.
                    
                    PythonScript has documentation in its Context-Sensitive Help
                    """
                    from Npp import *
                    
                    counter = 1
                    
                    def add_1(m):
                        global counter
                        repl = str(counter)
                        counter = counter + 1
                        return repl
                    
                    def disp_1(m):
                        console.write("found {}\n".format(m.group()))
                    
                    editor.rereplace(r'(?<=int\\[\\] arr)\d+', add_1)
                    

                    After pasting that and saving that, as described in the original instructions, what exactly were the steps you took? If it still doesn’t work with the correct final line in the code, could you go to Plugins > Python Script > Show Console and see if there are any error messages listed in the PythonScript console? and if there are, screenshot them and paste the image in your reply?

                    PeterJonesP 1 Reply Last reply Reply Quote 1
                    • PeterJonesP
                      PeterJones @PeterJones
                      last edited by

                      @B-MR ,

                      and here’s a video of it working for me:

                      1 Reply Last reply Reply Quote 1
                      • B MRB
                        B MR
                        last edited by

                        finally got it!
                        the problem was unnecessary space
                        thank u all !!

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