• Login
Community
  • Login

Massive list and massive search and replace?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
23 Posts 7 Posters 9.3k 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.
  • N
    Neil Schipper @Calvin Foo
    last edited by Neil Schipper Oct 26, 2022, 10:16 AM Oct 26, 2022, 9:54 AM

    @Calvin-Foo

    There’s very, very, very little to learn. First, follow the instructions in Alan’s second post to you (“REFERENCE” link) LIKE A MONKEY.

    Here’s a first script you can run:

    #! python
    import sys
    print "Old style print syntax"
    sys.stdout.write("Calvin-Foo's first script -- hello from Python %s\n" % (sys.version,))
    

    You don’t need to know what any of the lines mean or what they do. (Once you get it running, you can hack at it for fun).

    There’s a lot complexity in airplanes and elevators and keyboards and phones that you don’t see and don’t need to deal with. It’s really quite similar.

    1 Reply Last reply Reply Quote 1
    • A
      Alan Kilborn @Calvin Foo
      last edited by Alan Kilborn Oct 26, 2022, 12:12 PM Oct 26, 2022, 12:11 PM

      @Calvin-Foo said in Massive list and massive search and replace?:

      I guess I need to learn from ground up.
      I guess I just start from thereI’ll further study from there and include a list of text (maybe import from CSV)

      In case it isn’t obvious, you could take YOUR data, in whatever (textual) format, and use Notepad++ to change it with a replacement operation into MY demo format, and then just run with the demo solution.

      This avoids programming and could (probably) be made into a N++ macro for easy repetitive running.

      As an example, take your original problem statement data (I know it isn’t your real data, but we have none of that here, so…):

      1. james - James
      2. calvin - Calvin
      3. new york - New York
      

      You could change that into the needed input format for the script by this operation:

      Find: (?-s)^\d+\. (.+?) - (.+)
      Replace: ${1}->${2}
      Search mode: Regular expression
      Wrap around: Checked
      Action: Replace All button

      After the replace-all, your data would then look like this:

      james->James
      calvin->Calvin
      new york->New York
      

      which would be a direct feed-in to the demo script.


      this seems a bit too complicated for me

      Of course, this data transform may involve learning some regular expressions (don’t know your expertise), but I don’t think it is too much to ask people that request a moderately-complex solution to a problem to do some sort of learning of their own along the way.


      How to write a simple replace text script?

      Well about the simplest one I can think of would be a one-liner:

      editor.replace('apple', 'Apple')

      1 Reply Last reply Reply Quote 2
      • A Alan Kilborn referenced this topic on Nov 21, 2022, 6:42 PM
      • A Alan Kilborn referenced this topic on Nov 21, 2022, 7:53 PM
      • A Alan Kilborn referenced this topic on Jan 4, 2023, 1:10 PM
      • F fenzek1 referenced this topic on Jan 4, 2023, 3:19 PM
      • N
        nerdyone255
        last edited by Jun 7, 2024, 4:26 PM

        this script is FANTASTIC.

        i do have a question though- in the final output it prints how many replacements were made, but is there any way to see what the actual replacements were?

        looking into the code i see

        “”" if self.num_repl_made_in_this_file > 0:

                        self.print('replacing "{fw}" with "{rw}" {n} times'.format(
                            fw=find_what, rw=replace_with, n=self.num_repl_made_in_this_file)) """
        

        but i dont see where that would get printed- it doesnt show up in the python console either

        again huge thanks for this one

        A 1 Reply Last reply Jun 8, 2024, 1:19 AM Reply Quote 0
        • A
          Alan Kilborn @nerdyone255
          last edited by Alan Kilborn Jun 8, 2024, 1:21 AM Jun 8, 2024, 1:19 AM

          @nerdyone255 said :

          this script is FANTASTIC.

          Well…glad you like it.

          but i dont see where that would get printed- it doesnt show up in the python console either

          The self.print() function calls are really meant as debug helpers while testing the script. Thus, in the version of the script above, they don’t do anything because the debug variable is set to False. If you change the 0 to a 1 in this line:

          self.debug = True if 0 else False

          or simply change it to:

          self.debug = True

          then the output of the self.print() calls will go to the PythonScript console window. You’ll see the output you indicated you were interested, plus output from other things that happen while the script is running.

          N 1 Reply Last reply Jun 11, 2024, 1:39 PM Reply Quote 2
          • N
            nerdyone255 @Alan Kilborn
            last edited by Jun 11, 2024, 1:39 PM

            @Alan-Kilborn perfect!

            1 Reply Last reply Reply Quote 1
            • A Alan Kilborn referenced this topic on Aug 14, 2024, 10:42 AM
            • Y
              Yurble Vương @Alan Kilborn
              last edited by PeterJones Feb 9, 2025, 4:49 PM Feb 9, 2025, 4:41 PM

              @Alan-Kilborn

              Thanks Alan, it work perfectly, Except one specially for me. Appreciate your help if possible:

              I want to find only within word boundary.


              For example:
              Sentence: You are eating apple. The tree have a lot of apples. all the apples is green.
              apple->cherry
              apples->cherries


              Hence, how can I add in a code to made it change only words start or end a transition from space to non-space character (space, common, dot, quote marks, questions mark…).

              thanks in advance
              Yurble

              A 1 Reply Last reply Feb 9, 2025, 5:16 PM Reply Quote 0
              • A
                Alan Kilborn @Yurble Vương
                last edited by Feb 9, 2025, 5:16 PM

                @Yurble-Vương said in Massive list and massive search and replace?:

                how can I add in a code to made it change only words start or end a transition from space to non-space character

                You can use \b in the regular expression to insist upon a word boundary; example: \bapple will match have an apple today but will not match have a crabapple today.

                Y 2 Replies Last reply Feb 10, 2025, 2:20 PM Reply Quote 1
                • Y
                  Yurble Vương @Alan Kilborn
                  last edited by Feb 10, 2025, 2:20 PM

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • Y
                    Yurble Vương @Alan Kilborn
                    last edited by Yurble Vương Feb 10, 2025, 4:33 PM Feb 10, 2025, 2:55 PM

                    @Alan-Kilborn

                    Sorry to ask and bother you. I tried to edit your py code as below, but it seems to be a wrong code. Could you advise how to correct:

                    Not work:

                         # FINALLY, the actual replacement!
                                    editor.rereplace('\b'+find_what+'\b', replace_with)
                    

                    Not work 2:

                         # FINALLY, the actual replacement!
                                    editor.rereplace(r'\b'+find_what+'\b', replace_with)
                    
                    A 1 Reply Last reply Feb 11, 2025, 12:04 PM Reply Quote 1
                    • A
                      Alan Kilborn @Yurble Vương
                      last edited by Feb 11, 2025, 12:04 PM

                      @Yurble-Vương said :

                      Not work 2

                      This should work: editor.rereplace(r'\b'+find_what+r'\b', replace_with)

                      I like how you showed initiative in trying to solve the problem yourself…and you had the right idea, you just didn’t take it far enough.

                      Y 1 Reply Last reply Feb 11, 2025, 2:16 PM Reply Quote 1
                      • Y
                        Yurble Vương @Alan Kilborn
                        last edited by Feb 11, 2025, 2:16 PM

                        @Alan-Kilborn

                        Many thanks for help

                        1 Reply Last reply Reply Quote 0
                        • A Alan Kilborn referenced this topic on Feb 14, 2025, 1:46 AM
                        22 out of 23
                        • First post
                          22/23
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors