Are there any plugins or ways to perform multiple search and replace regex operations at once?
-
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?
-
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()
-
@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.
-
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?
-
@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-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. -
@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.
-
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.
-
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.
-
-
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()
-
-