Community
    • Login

    multiple text replacing with different text

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    20 Posts 3 Posters 5.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.
    • Handa FlockaH
      Handa Flocka
      last edited by Handa Flocka

      Hi @PeterJones
      Thank you and sorry for the confusion
      you explained my idea exactly for how I want it to be
      that was the same as what I meant,
      I will give a more example with the word USA instead of using the word KEYWORD this time.

      1- have you ever visited the USA
      2- the USA Freedom Act.
      3- she lives in the USA
      

      I want to replace the word “USA”
      To: my list contains different whole other countries names “by order”
      1- CANADA 2- MEXICO 3- COSTA RICA

      • have you ever visited the CANADA
      • the MEXICO Freedom Act.
      • she lives in the COSTA RICA

      same sentences just the “USA” changes

      let’s say in the list of words that I want to change to
      has more countries names more than what word “USA” is included in the text
      I guess it’s the logic to just change where the word “USA” is included or
      personally I would Accept whatever just to fix my current problem and save time

      1 Reply Last reply Reply Quote 0
      • Handa FlockaH
        Handa Flocka
        last edited by Handa Flocka

        If my list has an insufficient number of words than of how many times the word “USA” included the best would be not to repeat the words from 1- 2- again from the start and just finish replacing

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

          @Handa-Flocka ,

          regex solution

          This depends on how long your main data is; if it’s too long, the regex capture groups might run out of memory. But it might be worth trying.

          For my “special formatting” for your replacement list, I am going to use a line with ~~~~~~, then ∙ followed by each replacement string. (I picked ∙ because it’s not likely to be in your existing data; you can choose a different separator if you like; just make sure it’s not found elsewhere, and that you change it throughout these instructions

          1. Add the special formatting with the list at the end of your data:
            1- have you ever visited the USA
            2- the USA Freedom Act.
            3- she lives in the USA
            
            ~~~~~~
            ∙CANADA∙MEXICO∙COSTA RICA
            
          2. Hit Ctrl+HOME or equivalent to go to the first character in the file.
          3. Search > Replace
            • MODE = regular expression
            • ☑ WRAP AROUND = make sure it’s checked on
            • FIND = (?s)USA(.*?~+\s*)∙([^∙\r\n]*)
            • REPLACE = ${2}${1}
            • click FIND NEXT once, then hit REPLACE once for each replacement word (so three times for the three countries in the example). (REPLACE ALL will just replace the first instance because it’s capturing to nearly the end in order to delete each replace-text once it’s used)
          4. Manually delete the ~~~~~~ line from the end

          Python Script

          This requires installing the PythonScript plugin. (You can do that using Plugins Admin)

          1. Plugins > Python Script > New Script: saving as replaceList.py
          2. Use the following as the text of the document, and SAVE
            # encoding=utf-8
            """in response to https://community.notepad-plus-plus.org/topic/21225/
            
            Written by @PeterJones 
            Searches for keywordText multiple times, and replaces it with the next element of replacementList
            """
            from Npp import *
            
            keywordText = 'USA'
            replacementList = [ 'CANADA', 'MEXICO', 'COSTA RICA' ]
            
            counter = -1
            
            def nextReplacement(m):
                global counter
                counter += 1
                return replacementList[counter] if counter < len(replacementList) else m.string
            
            editor.beginUndoAction()
            editor.replace(keywordText, nextReplacement)
            editor.endUndoAction()
            
          3. Change the keywordText and replacementList variables to match your desired KEYWORD and list of replacements, using the same syntax as shown in the example. SAVE
          4. Open (or switch to) your file.
          5. Plugins > Python Script > Scripts > replaceList
          6. At this point, the replacement should be done. If you need to, a single UNDO should work to undo all the replacements

          That sequence works for me on your example data

          caveat emptor

          This script seemed to work for me, based on my understanding of your issue, and is published here to help you learn how to do this. I make no guarantees or warranties as to the functionality for you. You are responsible to save and backup all data before and after running this script. If you want to use it long term, I recommend investing time in adding error checking and verifying with edge cases.

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

            @PeterJones said in multiple text replacing with different text:

            Python Script

            Interesting; when I tried the script I got this error:

                return replacementList[counter] if counter < len(replacementList) else m.string
            <type 'exceptions.RuntimeError'>:  This method or property is not supported under Notepad++ PythonScript
            

            I would guess this is related to usage of m.string which I have never seen before. Maybe this is only supported in a P.S. that is newer than the one I use: 1.5.4

            Anyway, I tried out the script because I was going to suggest the following alternative method, and it just so happens that it avoids that error for me as well.

            Change the “return” line to:

            return replacementList[counter]

            and change the “.replace” line to:

            editor.replace(keywordText, nextReplacement, 0, 0, editor.getLength(), len(replacementList))

            This is instructive because it shows how to use PythonScript code to limit the number of replacements made; in this case, to the length of the replacement list.

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

              @Alan-Kilborn ,

              I am using PythonScript 1.5.4, so that’s not it
              3d6b6fe8-d2b6-4403-8748-812594697d04-image.png

              Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]
              Initialisation took 750ms
              Ready.
              

              Oh, wait, my example didn’t try to go beyond, so was never triggering the ELSE. when I add some more USA to the example data, then it does give me the error.

              I confirmed in the PythonScript 1.5.4 docs that its MatchObject doesn’t implement the re.MatchObject’s .string() method.
              52d4b53d-72ad-4ce5-a387-a60cfe15b314-image.png

              However, since the match string will always be identical to keywordText (because it’s a text search, not a regex search), if I change that line to
              return replacementList[counter] if counter < len(replacementList) else keywordText
              … then the error goes away and everything works even with too many USA instances.

              But yes, I like your idea of using the optional parameters to avoid having to do the conditional return.

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

                ah, also, instead of m.string(), I can use m.group(0) – group#0 is the whole match, even when it’s not a grouped or regex-based replacement.

                1 Reply Last reply Reply Quote 1
                • Handa FlockaH
                  Handa Flocka
                  last edited by

                  @PeterJones said in multiple text replacing with different text:

                  1- have you ever visited the USA
                  2- the USA Freedom Act.
                  3- she lives in the USA

                  thanks for the help @PeterJones much much appreciated
                  I have tried installing the python code without changing any of the text when I tried on this example:

                  1- have you ever visited the USA
                  2- the USA Freedom Act.
                  3- she lives in the USA
                  

                  it does not change anything

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

                    @Handa-Flocka said in multiple text replacing with different text:

                    it does not change anything

                    Can you tell me the exact steps you took, because it works for me.

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

                      … for example, here’s a screen-capture video:

                      1 Reply Last reply Reply Quote 1
                      • Handa FlockaH
                        Handa Flocka
                        last edited by Handa Flocka

                        Now it works like a charm!! I forgot some steps during the process

                        both of them now are working perfectly
                        it would be preferred to use python script instead of regex in this process if I am dealing with huge data?

                        Thank you for your help so much appreciation man

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

                          @Handa-Flocka said in multiple text replacing with different text:

                          it would be preferred to use python script instead of regex in this process if I am dealing with huge data?

                          Correct. Large amounts of data will mean that the (.*?~+\s*) capture groups grows beyond the regex capture-group memory allocated in Notepad++.

                          1 Reply Last reply Reply Quote 1
                          • Handa FlockaH
                            Handa Flocka
                            last edited by Handa Flocka

                            got it!
                            Thank you and for the effort you put on this

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

                              @Handa-Flocka ,

                              I am replying to your Chat message here. Questions like this belong in the Forum posts, not in the chat.

                              Sorry If bothering you
                              I have tried again to run the same py script on some random code it didn’t want to change
                              code

                              EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
                              

                              for example I am selecting word TYPEE
                              it doesn’t change at all

                              My guess is that the chat window got rid of the <open> tags on most of those. But the example included TYPEE, and that’s likely all that’s critical for the replacement.

                              1. Change replaceList.py so keywordText = 'TYPEE'; SAVE
                              2. Open file with example text
                                EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
                                
                              3. Plugins > PythonScript > Scripts > replaceList
                              4. Result
                                EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“CANADA” MODIFIERS=shift
                                
                                … So TYPEE has become CANADA

                              So it works for me.

                              1 Reply Last reply Reply Quote 1
                              • Handa FlockaH
                                Handa Flocka
                                last edited by

                                @PeterJones said in multiple text replacing with different text:

                                EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift

                                @PeterJones my bad I thought the thread was locked!
                                the code works perfectly in one line
                                but If I tried something like this for example

                                EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
                                EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
                                EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
                                EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
                                
                                

                                then it will not work!

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

                                  @Handa-Flocka said in multiple text replacing with different text:

                                  then it will not work!

                                  Sorry, I don’t know what you’re doing differently. With no change compared to my sequence above (that is, with TYPEE as the keyword and the same list of three countries as the replacementList), I get:

                                  EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“CANADA” MODIFIERS=shift
                                  EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“MEXICO” MODIFIERS=shift
                                  EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“COSTA RICA” MODIFIERS=shift
                                  EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
                                  

                                  Since my list only had 3 countries, the fourth TYPEE remains unchanged.

                                  Here is my exact replaceList.py with your data:

                                  # encoding=utf-8
                                  """in response to https://community.notepad-plus-plus.org/topic/21225/
                                  
                                  Written by @PeterJones
                                  
                                  Searches for the value of keywordText multiple times, and replaces it with the next element of replacementList
                                  """
                                  from Npp import *
                                  
                                  keywordText = 'TYPEE'
                                  replacementList = [ 'CANADA', 'MEXICO', 'COSTA RICA' ]
                                  
                                  counter = -1
                                  
                                  def nextReplacement(m):
                                      global counter
                                      counter += 1
                                      return replacementList[counter] if counter < len(replacementList) else m.group(0)
                                  
                                  editor.beginUndoAction()
                                  editor.replace(keywordText, nextReplacement)
                                  editor.endUndoAction()
                                  

                                  You might want to go to Plugins > PythonScript > Show Console and look for errors. (You can right click in the console window, click Clear, then run the script again on your data file. If anything is printed in the console, it’s an error message.) The error message will likely indicate where you have a typo or syntax error in the script that was introduced when you put in your new keywordText and replacementList. If you need help with that, copy/paste the errors you see into the forum (as well as your modified copy of replaceList.py)

                                  1 Reply Last reply Reply Quote 2
                                  • Handa FlockaH
                                    Handa Flocka
                                    last edited by Handa Flocka

                                    This post is deleted!
                                    1 Reply Last reply Reply Quote 0
                                    • Handa FlockaH
                                      Handa Flocka
                                      last edited by

                                      This post is deleted!
                                      1 Reply Last reply Reply Quote 0
                                      • Handa FlockaH
                                        Handa Flocka
                                        last edited by

                                        @PeterJones After your update it’s a piece of art now!
                                        thanks again

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