在notepad ++中进行多字搜索和替换

Al *_*edo 25 notepad++

有没有人知道如何在记事本++中一次性替换几个不同的单词.

例如;

我有"好","好"和"好",我想用"坏","更糟"和"不"全部替换它们

我知道我可以逐个替换它们,但我遇到的问题要求我更换很多单词,这样做不方便.

Adr*_*HHH 45

尝试使用正则表达式替换(good)|(great)|(fine)(?1bad)(?2worse)(?3not).

搜索查找由...分隔的三个备选项之一|.每个替代方案都有自己的捕获括号.replace使用条件形式?Ntrue-expression:false-expression其中N是十进制数字,该子句检查捕获表达式N是否匹配.

在Notepad ++ 6.3中测试过

更新:

您可以在以下两个地址找到关于N ++使用的新PRCE正则表达式的完整文档,自6.0版以来:

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

第一个涉及SEARCH中正则表达式的语法

SECOND涉及REPLACEMENT中正则表达式的语法

而且,如果您能理解"书面法语",我在下面的地址中制作了一个关于PCRE正则表达式的教程,存储在Christian Cuvier(cchris)的个人网站中:

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

(摘自THEVENOT Guyhttp://sourceforge.net/p/notepad-plus/discussion/331754/thread/ca059a0a/上发布的帖子)


Mau*_*les 23

从插件管理器安装Python脚本插件.

使用您的替换创建一个文件(例如,C:/Temp/Substitutions.txt),用空格分隔值:

good bad
great worse
fine not
Run Code Online (Sandbox Code Playgroud)

创建一个新脚本:

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

针对要替换的文本运行新脚本.

  • @Rishi出于这个问题的目的_simple search replace_就够了._Regex search replace_可以通过使用`editor.rereplace(s [0],s [1])`来实现.甚至更好的`editor.pyreplace`来利用完整的Python正则表达式支持. (3认同)

Kev*_*nTD 6

我四处寻找可以同时替换多个术语的软件,Notepad ++ 有字符限制(替换字段中的字符限制为 2046 个字符),所以我决定用 HTML 制作自己的替换版本,这里是网站:

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

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


function replace(){
	
	// Get Configuration
	let separatedBy = new RegExp( $("#separatedBy").val() );
	let useRegex = $("#useRegex").prop("checked");
	let isCaseSensitive = $("#caseSensitive").prop("checked") ? "" : "i";
	
	// Get 'Find' and 'Replace'
	let find = $('#find').val().split(separatedBy);
	let replace = $('#replace').val().split(separatedBy);
	
	// Make result equal origin
	$("#result").val($("#original").val());
	
	// Run in each 'find' item
	for(i=0; i<find.length; i++){
		// check if not use Regex to escape the regex
		find[i] = useRegex ? find[i] : find[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
		
		// Replace
		$("#result").val($("#result").val().replace(new RegExp(find[i],"g"+isCaseSensitive), replace[i]));
	}
	
}
Run Code Online (Sandbox Code Playgroud)
*{
	font-family: Sans-Serif;
	box-sizing: border-box;
}

body{
	padding:1em 10%;
}

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

textarea{
	border-radius: 5px;
	width: 100%;
	padding:1em;
}

.config{
	width: 100%;
	border-radius: 5px;
	border: 1px solid #888888;
	padding: 1em;
	display: inline-block;
	color: #888888;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.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="find" rows="4" cols="50">item1
item2
item3</textarea>
</p>

<p>
	Items to replace:<br />
<textarea id="replace" rows="4" cols="50">replace1
replace2
replace3</textarea>
</p>

<p class="config">
	<b>Configuration:</b><br><br />
	- Items separated by <input id="separatedBy" type="text" value="\n" size="3" /> (Regex)<br />
	- <input id="useRegex" type="checkbox"/> Use Regex in each item<br>
	- <input id="caseSensitive" type="checkbox" checked/> Use case sensitive
</p>

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

<p>
	Result:<br>
	<textarea id="result"  rows="4" cols="50"></textarea>
</p>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!那真的很有帮助。对于 16 个单词的列表效果很好。 (2认同)
  • “所以我决定制作自己的替代版本”...那是我的偶像..编码良好,共享且闪耀。(: (2认同)