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