Multiple word search and replace in notepad++

2020-01-23 07:46发布

问题:

Does anyone know how to 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" 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.

回答1:

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.



回答2:

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/ )



回答3:

I looked around for some software that would replace several terms at the same time, Notepad ++ has a character limitation (limit of 2046 characters in the replace field), so I decided to make my own replacement version in HTML, here is the site:

https://jsfiddle.net/Byte/y50q1e7g/

window.onload=function(){
	$("textarea,input[type='text']").focus(function() { $(this).select(); } );
}


function substituira() {
	
	var separatedBy = new RegExp($("#separatedBy").val());
	
	var achar = $('#achar').val().split(separatedBy);
	var substituir = $('#substituir').val().split(separatedBy);
	
	$("#final").val($("#original").val());
	
	var isCaseSensitive = "";
	if(!$( "#caseSensitive" ).prop("checked")){isCaseSensitive = "i";}
	
	for(i=0;i<achar.length;i++){
		$("#final").val($("#final").val().replace(new RegExp(achar[i],"g"+isCaseSensitive), substituir[i]));
	}
}
*{
	font-family: Sans-Serif;
}

body{
	padding:1em 10%;
}

textarea,input[type='text']{
	font-family: monospace;
}

textarea{
	border-radius: 5px;
}

.config{
	border-radius: 5px;
	border: 1px solid #888888;
	padding: 1em;
	display: inline-block;
	color: #888888;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Original text:<br /><textarea id="original" rows="4" cols="50"></textarea></p>

<p>Items to find:<br /><textarea id="achar" rows="4" cols="50">item1
item2
item3</textarea></p>
<p>Items to replace:<br /><textarea id="substituir" rows="4" cols="50">replace1
replace2
replace3</textarea></p>

<p class="config"><b>Configuration:</b><br>
Items separated by <input id="separatedBy" type="text" value="\n" size="3" /><br />
<input id="caseSensitive" type="checkbox"/> Case sensitive<br><br>
<i style="font-size: 0.7em;">NOTE: Already supports regex on each item</i></p><br /><br />



<input type="button" value="Replace" onclick="substituira()" />

<p>Result:<br><textarea id="final"  rows="4" cols="50"></textarea></p>



回答4:

I needed to run the substitution on several files.

So, based on Mauricio Morales's answer, I created the follow 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


回答5:

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



标签: notepad++