使用记事本++搜索并替换每个文件的第一个结果 [英] Search and replace only first result on each file with notepad ++

查看:56
本文介绍了使用记事本++搜索并替换每个文件的第一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以找到在每个文件中重复多次但只替换每个文件中的第一个结果的字符串吗?

Is there any way to find a string which is repeated several times in each file but only replace the first result on each file?

谢谢

推荐答案

Notepad++ 的查找对话框包括一个在文件中查找 选项卡和一个在文件中替换 按钮.要将所有文件中的 abc 替换为 def,您可以尝试以下正则表达式搜索字符串 \A(.*?)abc(.*)\z\1ghi\2.您需要同时选择正则表达式点匹配换行符.

The find dialogue of Notepad++ includes a Find in files tab with a Replace in files button. To replace abc with def in all files you could try the following regular expression search string \A(.*?)abc(.*)\z with \1ghi\2. You will need to select both Regular expression and Dot matches newline.

\A 只匹配文件的最开始.(.*?)abc 是一个非贪婪匹配和捕获所有内容,直到但不包括第一个 abc.(.*) 匹配并捕获到与 \z 匹配的文件末尾的所有其他内容.(我在没有 (.*)\z 部分的情况下进行了实验,并且所有出现的 abc 都被更改了.)

The \A only matches at the very start of the file. The (.*?)abc is a non-greedy match and capture of everything up to but not including the first abc. The (.*) matches and captures everything else to the end of the file which is matched by the \z. (I experimented without the (.*)\z part and all occurrences of abc were changed.)

如果要进行的替换还需要正则表达式,那么您可能需要更改替换文本的 \2 部分.

If the replacement to be done also needs regular expressions then you may need to alter the \2 part of the replacement text.

不知道这将如何处理大文件.无论您使用什么大小的文件,我都建议您在使用替换文件工具之前进行备份.

Do not know how this would work with big files. Whatever size of file you use I recommend making a backup before using the Replace in files facility.

在 Notepad++ 6.3.2 中使用两个非常小的文件进行测试.

Tested in Notepad++ 6.3.2 with two very small files.

这篇关于使用记事本++搜索并替换每个文件的第一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆