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.1k 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.
    • Hellena CrainicuH
      Hellena Crainicu
      last edited by Hellena Crainicu

      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?

      Alan KilbornA 1 Reply Last reply Reply Quote 1
      • Alan KilbornA
        Alan Kilborn @Hellena Crainicu
        last edited by Alan Kilborn

        @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()
        
        Hellena CrainicuH 1 Reply Last reply Reply Quote 4
        • Hellena CrainicuH
          Hellena Crainicu @Alan Kilborn
          last edited by

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

          Alan KilbornA 2 Replies Last reply Reply Quote 0
          • Robin CruiseR
            Robin Cruise
            last edited by

            @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?

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Robin Cruise
              last edited by

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

              Hellena CrainicuH 1 Reply Last reply Reply Quote 1
              • Alan KilbornA
                Alan Kilborn @Hellena Crainicu
                last edited by Alan Kilborn

                @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
                • Alan KilbornA
                  Alan Kilborn @Hellena Crainicu
                  last edited by

                  @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
                  • Hellena CrainicuH
                    Hellena Crainicu @Alan Kilborn
                    last edited by

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

                    EkopalypseE 1 Reply Last reply Reply Quote 0
                    • EkopalypseE
                      Ekopalypse @Hellena Crainicu
                      last edited by

                      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
                      • Alan KilbornA Alan Kilborn referenced this topic on
                      • rodica FR
                        rodica F
                        last edited by

                        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
                        • Alan KilbornA Alan Kilborn referenced this topic on
                        • Alan KilbornA Alan Kilborn referenced this topic on
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors