如何在Notepad++中合并含指定关键词的行至对应首词行
用Notepad++实现关键词行合并需求
需求说明
我有一份按行排列的文本,部分行以「关键词+|」作为前缀。需要将任意位置包含该关键词的行,合并到以该关键词为开头的对应行中,并将合并行里的关键词替换为*关键词#*格式。
示例输入
large text|The,first,word,on,some,lines,end,with,this,sign other|texts,below,it,contain,this,word,anywhere,in,the,line what is required|merge,the,lines,containing,this,word،into,the,line Any text is here then large text another word after that So other texts here contain this word anywhere in the line the end what is required to merge the lines
期望输出
large text|The,first,word,on,some,lines,end,with,this,sign Any text is here then *large text#* another word after that other|texts,below,it,contain,this,word,anywhere,in,the,line So *other#* texts here contain this word anywhere in the line what is required|merge,the,lines,containing,this,word،into,the,line the end *what is required#* to merge the lines
实现步骤
通过Notepad++的正则表达式替换功能可以实现,操作如下:
- 打开目标文本,按下
Ctrl+H调出替换面板 - 在「查找模式」中选择「正则表达式」,并勾选「.匹配换行符」选项
- 在「查找内容」框中输入:
^(.+?)\|(.+)\R(.*?)\1(.*)$ - 在「替换为」框中输入:
\1|\2 \3*\1#*\4 - 点击「全部替换」按钮,如果存在一个关键词对应多行的情况,重复点击直到没有新的替换结果为止
正则说明
^(.+?)\|(.+)\R:匹配以「关键词+|」开头的行,捕获关键词为\1,捕获该行|后的内容为\2,\R匹配换行符(.*?)\1(.*)$:匹配包含关键词\1的后续行,捕获关键词前后的文本内容为\3和\4- 替换规则会将前缀行与对应行合并,并把原行中的关键词替换为
*关键词#*格式
内容的提问来源于stack exchange,提问作者amr
相关产品推荐
相关产品推荐

