我在文件中有以下内容:
This is line one
This is line two with string1 and string3
This is line three with string3如果行包含‘string1 3’,我需要将'string1‘替换为'string2’,这样结果将是:
This is line one
This is line two with string2 and string3
This is line three with string3发布于 2017-05-18 12:52:04
若要替换该行上任何包含string1的行中出现的所有string3,需要使用基于\G的正则表达式:
(?:\G(?!^)|^(?=.*string3)).*?\Kstring1用string2代替。见regex在线演示。
详细信息
(?:\G(?!^)|^(?=.*string3)) -要么是上一次匹配的结束,要么是包含string3的行的开始.*? -除换行之外的任何0+字符\K -在当前迭代中丢弃所有匹配的文本的匹配重置操作符string1 -要替换的string1子字符串。以下案文
This is line string1
This is line two with string1 and string3 string1
This is line two with string1 string1 and string3
This is line three with string3
This is line string3 with string1 and string1转成

发布于 2017-05-18 11:29:19
代表@Gawil
在Notepad++上转到搜索->替换
在“查找什么”下:^(?=.*?string3)(.*?)string1(.*?)$
在“替换为”:\1string2\2下面
根据需要执行“替换”或“替换所有”。

https://stackoverflow.com/questions/44040505
复制相似问题