• Login
Community
  • Login

Please need a multiple renewable change

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 4 Posters 388 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.
  • P
    patrick rozon
    last edited by Aug 7, 2021, 10:25 PM

    Hello,

    I have a text in its content contains a character ‘&’ repeated several times, I want to replace each & that exists in the text by :

    In the 1st & replace by 1
    In the 2nd & replace with 2
    the 3rd & replace with 3
    the 4th & replace with 4
    the 5th & replace with 5
    the 6th & replace with 6
    And the rest of ‘&’ that are present in the text:

    Put in the 7th &, the same replacement that I gave before to the 1st & which is 1, and so on for the other ‘&’:

    Put in the 8th &’ : the same replacement I gave to the 2nd ‘&’ which is 2

    In the 9th &’ : the replacement of the 3rd ‘&’ which is 3
    In the 10th &’ : the replacement of the 4th ‘&’ which is 4
    in the 11th &’ : replacement of the 5th ‘&’ which is 5
    the 12th &’ : the replacement of the 6th ‘&’ which is 6

    In the 13th & : the replacement will be 1
    the 14th & : the replacement will be 2
    the 15th & by : 3
    the 16th & by : 4
    the 17th & by : 5
    the 18th & by : 6

    And so on; always a renewal of this operation described above for the rest of & that are in the text.

    Can someone help me ?

    And thank you for your help.

    P 1 Reply Last reply Aug 7, 2021, 10:57 PM Reply Quote 0
    • P
      PeterJones @patrick rozon
      last edited by Aug 7, 2021, 10:57 PM

      @patrick-rozon

      Not natively in stock Notepad++.
      If you have the PythonScript plugin, or ar willing to install it, it’s easy, using the “add_1” style replacement found in the PythonScript documentation.

      Here’s one such implementation; the instructions for installing PythonScript, creating the script, and running the script are embedded in the comments of the script.

      # 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 NReplacements.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 > NReplacements
      
      You can change the replacement values, or the number of replacements,
      then save, and it will still run.
      
      I used editor.replace() so that & wouldn't have special meaning.
      If you need to use a regular expression to match your "to-be-matched" text, use editor.rereplace instead.
      
      PythonScript has documentation in it's Context-Sensitive Help
      """
      from Npp import *
      
      counter = 0
      replacements = ('1','2','3','4','5','6')
      
      def add_1(m):
          global counter
          repl = replacements[counter]
          counter = (counter + 1) % len(replacements)
          return repl
      
      editor.replace(r'&', add_1) # use editor.rereplace if the first term needs to be a regex
      

      Please note that & has special meaning for regexes, so I used the non-regex editor.replace(); normally, I would just use editor.rereplace(r'...', add_1) so that when the end user decides they need a more powerful search, all they have to do is change the value, without having to remember to change the replace to rereplace.

      P 1 Reply Last reply Aug 7, 2021, 11:27 PM Reply Quote 2
      • P
        patrick rozon @PeterJones
        last edited by Aug 7, 2021, 11:27 PM

        @PeterJones

        Thank you dear PeterJones for your quick response,

        I have installed PythonScript plugin, the script works very well as desired, thank you very much for your support.

        1 Reply Last reply Reply Quote 0
        • T
          Terry R
          last edited by Aug 8, 2021, 9:14 PM

          @patrick-rozon @PeterJones

          Although the solution has been found (in PythonScript) it does appear to be possible with regular expression (regex) as well, that is if I read the question correctly.

          The regex isn’t too daunting to understand and indeed it shows a reoccurring sequence so could easily be extended to whatever modulo was used (currently the number 6 requested).
          This is using the Find function with search mode set to “regular expression”.
          Find What:(([^&]*)&)?(([^&]*)&)?(([^&]*)&)?(([^&]*)&)?(([^&]*)&)?(([^&]*)&)?
          Replace With:(?{1}${2}1)(?{3}${4}2)(?{5}${6}3)(?{7}${8}4)(?{9}${10}5)(?{11}${12}6)

          With “Wrap around” ticked and the "Replace All button pressed it will commence at the start of the file and find the first group of up to 6 & with (or without) characters around them. It will then replace with the numbers 1 through 6 respectively. It then repeats finding the next sequence of up to 6 & and so on.

          So for a description we have:
          (([^&]*)&)? - means look for some non & characters (or not, that’s the *) followed by a & character. The ? allows for this capture group to be skipped if it does not find the right characters, this allows for a last sequence of less than 6 & to be changed. This is then repeated the number of times required (6 in this case).
          (?{1}${2}1) - means if group 1 DID capture some characters then we return them without the & (that’s group 2) and then follow that string with a number, in this case 1. By repeating this sequence and incrementing the group numbers and the following number as a replacement we will respectively replace up to 6 & with the corresponding new number (1 through 6).

          Terry

          1 Reply Last reply Reply Quote 2
          • G
            guy038
            last edited by guy038 Aug 10, 2021, 9:09 AM Aug 10, 2021, 9:06 AM

            Hello, @patrick-rozon , @peterjones , @terry-r and All,

            Here is my solution. Not ideal, of course, as it would be suitable for small file size, only, but quite flexible !. It also uses a dummy character, not present in current file. I presonally chose the ¤ char of my French keyboard but, of course, any other char may be used

            • Add a line ¤123456 at the very end of current file

            • SEARCH (?s)&(.*¤)(\d)(\d+)|¤\d+

            • REPLACE ?1\2\1\3\2

            • Tick the Wrap around option

            • Select the Regular expression seardh mode

            • Hit, repeatedly, on Alt + R ( Replace button ) OR on Alt + A ( Replace All button ) till one of the follwing messages occurs :

              • Replace: one occurrence was replaced. No more occurrences were found.

              • Replae All: 0 occurrences were replaced in entire file


            One advantage of this method is that you may change, either, the modudlo and the values, without changing the regex S/R ! For instance, let’s suppose you want to replace any & char, by any even number ( so, modulo 5 ), in decreasing order, Then, simply add the line ¤86420 at the very end of the file and just use the regex S/R mentionned above !

            So, from the INPUT text :

            ------------&--------------&----&&-----------------&----------------&-----
            ------------&--------------&----&&-----------------&----------------&-----
            ------------&--------------&----&&-----------------&----------------&-----
            ¤86420
            

            After 19 replaceemnts, we would get the OUTPUT text :

            ------------8--------------6----42-----------------0----------------8-----
            ------------6--------------4----20-----------------8----------------6-----
            ------------4--------------2----08-----------------6----------------4-----
            

            Best Regards,

            guy038

            1 Reply Last reply Reply Quote 2
            • T Terry R referenced this topic on Oct 20, 2022, 2:25 AM
            2 out of 5
            • First post
              2/5
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors