Reputation: 471
How can I replace several different words all at once in Notepad++?
For example;
I have "good", "great" and "fine" and I want to replace them with "bad", "worse" and "not", respectively, all at once.
I know that I can replace them one by one, but the problem I am facing requires that I replace a lot of words, which is not convenient to do.
Upvotes: 37
Views: 70995
Reputation: 14047
Try a regular expression replace of (good)|(great)|(fine)
with (?1bad)(?2worse)(?3not)
.
The search looks for either of three alternatives separated by the |
. Each alternative has its own capture brackets. The replace uses the conditional form ?Ntrue-expression:false-expression where N is decimal digit, the clause checks whether capture expression N matches.
Tested in Notepad++ 8.5.8 (and previously in Notepad++ 6.3).
Since this answer was first written the Notepad community has created the Notepad++ Online User Manual and it contains a good description of the Substitution Conditionals used here.
The replacement string above can be extended to have replacements for the false-expressions. For example (?1bad:[1])(?2worse:[2])(?3not:[3])
. These false expressions are not very useful, but they provide a demonstration of what is happening in each replacement. Using this replacement on the string:
+good+great+fine+
yields the result:
+bad[2][3]+[1]worse[3]+[1][2]not+
The original answer includes the information below. The three links checked and are still valid on 2024-01-02, the ZIP file was not checked:
You can find good documentation, about the new PRCE Regular Expressions, used by N++, since the 6.0 version, at the TWO addresses below :
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
The FIRST one concerns the syntax of regular expressions in SEARCH
The SECOND one concerns the syntax of regular expressions in REPLACEMENT
And, if you can understand "written French", I made a tutorial about PCRE regular expressions, stored in the personal site of Christian Cuvier (cchris), at the address below :
(Extracted from a posting by THEVENOT Guy at http://sourceforge.net/p/notepad-plus/discussion/331754/thread/ca059a0a/ )
Upvotes: 72
Reputation: 3015
I needed to run the substitution on several files. Based on Mauricio Morales's answer, I created the following script.
with open('C:/Temp/Substitutions.txt') as f:
files = notepad.getFiles()
for file in files:
notepad.activateFile(file[0])
for l in f:
s = l.split()
editor.replace(s[0], s[1])
f.seek(0) # Reset file input stream
Upvotes: 2
Reputation: 1
If you're replacing the same words in several different files all the time, recording your action once using these buttons and saving it as a macro will be helpful. *Notepad++
Upvotes: -1
Reputation: 1105
good bad
great worse
fine not
with open('C:/Temp/Substitutions.txt') as f:
for l in f:
s = l.split()
editor.replace(s[0], s[1])
Upvotes: 32