• Login
Community
  • Login

Are there any plugins or ways to perform multiple search and replace regex operations at once?

Scheduled Pinned Locked Moved General Discussion
10 Posts 5 Posters 2.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.
  • H
    Hellena Crainicu
    last edited by Hellena Crainicu Feb 20, 2022, 6:36 PM Feb 20, 2022, 6:35 PM

    Are there any plugins or ways to perform multiple search and replace operations at once?

    Let’s say I have several FIND and REPLACE regex operations. But I don’t have time to run each one. I would like to put each operation in order, then run the program once (and the program will run them in the order I put them).

    Is it possible? Has anyone thought about this?

    A 1 Reply Last reply Feb 20, 2022, 7:00 PM Reply Quote 1
    • A
      Alan Kilborn @Hellena Crainicu
      last edited by Alan Kilborn Feb 20, 2022, 7:01 PM Feb 20, 2022, 7:00 PM

      @hellena-crainicu

      It’s not difficult, if you’re willing to take the leap to scripting.

      For example, here’s some PythonScript code that does what you ask for:

      # -*- coding: utf-8 -*-
      from __future__ import print_function
      
      from Npp import *
      
      #-------------------------------------------------------------------------------
      
      class T22601(object):
      
          def __init__(self):
              regex_find_repl_dict_list = [
                  { 'find' : r'foo', 'replace' : r'bar' },
                  { 'find' : r'fu', 'replace' : r'bar' },
              ]
              editor.beginUndoAction()
              for d in regex_find_repl_dict_list: editor.rereplace(d['find'], d['replace'])
              editor.endUndoAction()
      
      #-------------------------------------------------------------------------------
      
      if __name__ == '__main__': T22601()
      
      H 1 Reply Last reply Feb 20, 2022, 9:10 PM Reply Quote 4
      • H
        Hellena Crainicu @Alan Kilborn
        last edited by Feb 20, 2022, 9:10 PM

        @alan-kilborn thank you.

        Is this code working only for Notepad++ -> Menu -> Plugins -> Python Script ?

        Or it works for a general Python platform?

        Because, for usual Python platform some regex operators such as \K or \A …and many other combination of formulas, doesn’t work.

        Only in Notepad++ or Sublime Text or GrepWin works all regex operators and formulas.

        A 2 Replies Last reply Feb 20, 2022, 9:33 PM Reply Quote 0
        • R
          Robin Cruise
          last edited by Feb 20, 2022, 9:23 PM

          @alan-kilborn

          how can your script/code be used if someone want to change many text files from one folder? Don’t I have to put the directory path somewhere?

          A 1 Reply Last reply Feb 20, 2022, 9:31 PM Reply Quote 0
          • A
            Alan Kilborn @Robin Cruise
            last edited by Feb 20, 2022, 9:31 PM

            @robin-cruise said in Are there any plugins or ways to perform multiple search and replace regex operations at once?:

            how can your script/code be used if someone want to change many text files from one folder? Don’t I have to put the directory path somewhere?

            The OP didn’t say anything about multiple files, just multiple operations. Consequently, the script doesn’t consider multiple files.

            H 1 Reply Last reply Feb 21, 2022, 8:09 AM Reply Quote 1
            • A
              Alan Kilborn @Hellena Crainicu
              last edited by Alan Kilborn Feb 20, 2022, 9:34 PM Feb 20, 2022, 9:33 PM

              @hellena-crainicu said in Are there any plugins or ways to perform multiple search and replace regex operations at once?:

              Or it works for a general Python platform?

              The code is definitely tied to PythonScript, not a “general Python platform”.
              Otherwise it would be off-topic for this forum.

              1 Reply Last reply Reply Quote 1
              • A
                Alan Kilborn @Hellena Crainicu
                last edited by Feb 20, 2022, 9:38 PM

                @hellena-crainicu said in Are there any plugins or ways to perform multiple search and replace regex operations at once?:

                Only in Notepad++ or Sublime Text or GrepWin works all regex operators and formulas.

                I presume that if that is true then all of these use the Boost regex engine.

                1 Reply Last reply Reply Quote 1
                • H
                  Hellena Crainicu @Alan Kilborn
                  last edited by Feb 21, 2022, 8:09 AM

                  @alan-kilborn

                  The OP didn’t say anything about multiple files, just multiple operations. Consequently, the script doesn’t consider multiple files.

                  yes, indeed. Good point of view. It forgot to mention. How can I make multiple files and replace, in a folder with many text files?

                  This will be a great solution, will help a lot.

                  E 1 Reply Last reply Feb 21, 2022, 10:00 AM Reply Quote 0
                  • E
                    Ekopalypse @Hellena Crainicu
                    last edited by Feb 21, 2022, 10:00 AM

                    One way such a thing can be implemented is shown here, for example.

                    In order to use the editor’s methods, each of the files must be loaded, otherwise scintilla, the underlying component to which the editor object refers, has no way of accessing the text content to perform, for example, the searches.

                    1 Reply Last reply Reply Quote 3
                    • A Alan Kilborn referenced this topic on Mar 16, 2022, 11:35 AM
                    • R
                      rodica F
                      last edited by Apr 10, 2022, 3:00 PM

                      Done. If someone wants to make search and replace with Python Script, in a directory:

                      # -*- coding: utf-8 -*-
                      from __future__ import print_function
                      
                      from Npp import *
                      import os
                      import sys
                      
                      #-------------------------------------------------------------------------------
                      
                      class T22601(object):
                      
                          def __init__(self):
                              Path="C:\\python-test"
                              for root, dirs, files in os.walk(Path):
                                  nested_levels = root.split('/')
                                  if len(nested_levels) > 0:
                                      del dirs[:]
                                  for filename in files:		
                                      if filename[-5:] == '.html':
                                          notepad.open(root + "\\" + filename)
                                          console.write(root + "\\" + filename + "\r\n")
                                          notepad.runMenuCommand("Encodage", "Convertir en UTF-8")
                                          regex_find_repl_dict_list = [
                                              { 'find' : r'foo', 'replace' : r'bar' },
                                              { 'find' : r'bar', 'replace' : r'gaga' },
                                          ]
                                          editor.beginUndoAction()
                                          for d in regex_find_repl_dict_list: editor.rereplace(d['find'], d['replace'])
                                          editor.endUndoAction()
                                          notepad.save()
                                          notepad.close()
                      #-------------------------------------------------------------------------------
                      
                      if __name__ == '__main__': T22601()
                      
                      1 Reply Last reply Reply Quote 1
                      • A Alan Kilborn referenced this topic on Sep 18, 2022, 12:35 PM
                      • A Alan Kilborn referenced this topic on Oct 24, 2022, 2:00 PM
                      4 out of 10
                      • First post
                        4/10
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors