• Login
Community
  • Login

re-numbering phrase+number in the middle of sentence

Scheduled Pinned Locked Moved General Discussion
10 Posts 3 Posters 827 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
    B MR
    last edited by Aug 24, 2021, 3:32 PM

    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};
    
    P 1 Reply Last reply Aug 24, 2021, 3:47 PM Reply Quote 0
    • P
      PeterJones @B MR
      last edited by Aug 24, 2021, 3:47 PM

      @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};
      
      A 1 Reply Last reply Aug 25, 2021, 12:32 PM Reply Quote 2
      • B
        B MR
        last edited by Aug 25, 2021, 12:14 PM

        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
        • A
          Alan Kilborn @PeterJones
          last edited by Aug 25, 2021, 12:32 PM

          @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
            B MR
            last edited by Aug 25, 2021, 12:40 PM

            @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
              B MR
              last edited by Aug 26, 2021, 11:19 AM

              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)

              A P 2 Replies Last reply Aug 26, 2021, 11:30 AM Reply Quote 0
              • A
                Alan Kilborn @B MR
                last edited by Aug 26, 2021, 11:30 AM

                @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
                • P
                  PeterJones @B MR
                  last edited by Aug 26, 2021, 1:12 PM

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

                  P 1 Reply Last reply Aug 26, 2021, 1:19 PM Reply Quote 1
                  • P
                    PeterJones @PeterJones
                    last edited by Aug 26, 2021, 1:19 PM

                    @B-MR ,

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

                    1 Reply Last reply Reply Quote 1
                    • B
                      B MR
                      last edited by Aug 26, 2021, 6:49 PM

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

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