36

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
didiFaridi
  • 461
  • 1
  • 6
  • 12

4 Answers4

68

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 ist 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++ 6.3

Update:

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 :

http://oedoc.free.fr/Regex/TutorielRegex.zip

(Extracted from a posting by THEVENOT Guy at http://sourceforge.net/p/notepad-plus/discussion/331754/thread/ca059a0a/ )

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
  • Amazing! Could you add the source? I could not find this anywhere in the documentation. I use notepad++'s regex replace frequently and I'm curious if I could get more some cool stuff – Rishi Dua Jun 18 '13 at 08:10
  • index start at 0 for me in 6.4.5 – jpsimard-nyx Feb 18 '14 at 20:09
  • awesome never knew about this, use notepad++ for regex replace constantly. – OGHaza Mar 12 '14 at 16:45
  • This is awesome! But, do you know how to make it use the output of the previous search/replace for the next one? I assumed that's what it was doing because of the |, but it's not. – Patches Sep 18 '14 at 14:45
  • But how could we do this for a huge list? I have about 100 elements separated by ,'s and the other list separated by a comma too. – AbstractDissonance Mar 14 '17 at 18:25
30

Install Python Script plugin from Plugin Manager.

Create a file with your substitutions (e.g., C:/Temp/Substitutions.txt), separate values with space:

good bad
great worse
fine not

Create a new script:

with open('C:/Temp/Substitutions.txt') as f:
    for l in f:
        s = l.split()
        editor.replace(s[0], s[1])

Run the new script against the text you want to substitute.

Mauricio Morales
  • 1,075
  • 12
  • 14
2

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
D.Kastier
  • 2,640
  • 3
  • 25
  • 40
-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++