在Notepad ++中查找并替换为特定文本

问题描述

我有一个xml文件,需要对元素进行一些更改。

<someElement name="Sometext">

我想用这样的属性修改上面的内容

<someElement name="Sometext" newattribute="Sometext_8c54">

在上面,我只想复制 name 属性并创建一个 newattribute ,其值为 name 属性,并带有 _4digitrandomhexadecimal

如何在记事本++中实现此目标

解决方法

如果使用 EmEditor ,请按 Ctrl + H 弹出 Replace 对话框,然后输入:

查找<(.*?)name="(.*?)">

替换为\J "<\1name=\"\2\" newattribute=\"\2_" + (Math.floor(Math.random()*16)).toString(16) + (Math.floor(Math.random()*16)).toString(16) + (Math.floor(Math.random()*16)).toString(16) + (Math.floor(Math.random()*16)).toString(16) + "\">"

设置正则表达式选项,然后单击全部替换

EmEditor Replace dialog box

说明

替换为字符串开头的

\J表示它是JavaScript。 \1\2是反向引用,指向匹配字符串中的第一个(.*?)和第二个(.*?)(Math.floor(Math.random()*16)).toString(16)是随机的十六进制字符。

参考:http://www.emeditor.org/en/howto_search_replacement_expression_syntax.html