如何在多个文件中仅替换notepad ++中的第一个文本实例? [英] How to replace only first instance of text in notepad++ in multiple files?
问题描述
我正在通过一些更改更新网站,并且我设法使用标准查找和替换搜索对多个文件进行了大部分更改.我的 f/r 只剩下一个错误需要修复,但它在整个文档中发生了好几次,我只想替换第一个实例,唯一一次是错误.
I'm working on updating a website with a few changes and I've managed to do most of the changes to multiple files using standard find and replace searches. There is just one error left over from my f/r that I need to fix but it occurs several times throughout the document and I want to replace only the first instance, the only time it's an error.
</div><div class="boxb">
这在每个文件中出现七次,只有第一次我需要删除尾随的、关闭的 DIV.所有其他都是正确的.
That occurs seven times in each file, only the FIRST time I need to remove the trailing, closing DIV. All the others are correct.
我认为我需要使用正则表达式模式,但我已经尝试了一些示例,但它们要么不起作用,要么仍然替换所有出现的情况,而不仅仅是我需要的第一个.
I gather I need to use the Regular Expression mode but I've tried a few examples and they either don't work or they still replace all occurences, not just the first as I require.
还要明确一点,我不知道这是否有区别,但我需要对多个文件执行此操作,因此使用在文件中查找"而不仅仅是替换".
Also to be clear, I don't know if it makes a difference, but I need to do this to multiple files, so using 'Find In Files' rather than just 'Replace'.
谢谢
推荐答案
一种方法是匹配文件的全部内容,然后将您仍要保留的部分替换回去.这确保整个文件产生恰好 1 个匹配项.此方法尚未测试,但在大文件上可能会很慢.
One way is to match the whole content of the file, then replace back the parts that you still want to keep. This ensure that the whole file produces exactly 1 match. This method is not yet tested, but it might be slow on big file.
按照正常写正则表达式,然后在前面加上
(.*?)
,在后面加上(.*)
:
(.*?)pattern(.*)
您应该在几个文件上使用 (.*?)pattern
进行测试,以确保它与您要删除的文本的第一个实例匹配.
You should test with (.*?)pattern
on a few files to make sure that it matches the first instance of the text you want to remove.
选中复选框.
匹配换行符".
Check the checkbox ".
matches newline".
替换字符串 可能因您的正则表达式而异,但其想法是捕获您想要保留在步骤 1 的正则表达式中的部分,并在此步骤中将它们替换回.
The replacement string may vary depending on your regex, but the idea is to capture the parts that you want to keep in the regex of step 1 and replace them back in this step.
例如,如果您想删除任何与 pattern
匹配的内容,则使用 $1$2
作为替换字符串.
For example, if you want to remove anything that matches pattern
, then use $1$2
as replacement string.
这篇关于如何在多个文件中仅替换notepad ++中的第一个文本实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!