我有一个 xml 文件,我想用子节点中的一些文本替换父节点中的文本。这是一个示例 xml:
<output name="Element_152">
<field name="NAME" key="true">Unit001</field>
<field name="ITEM">CommStatus</field>
</output>
<output name="Element_153">
<field name="NAME" key="true">Unit001</field>
<field name="ITEM">UnitStatus</field>
</output>
我想将属性的值更改<output name="Element_...">
为来自的值<field name="ITEM">
,因此我将得到:
<output name="CommStatus">
<field name="NAME" key="true">Unit001</field>
<field name="ITEM">CommStatus</field>
</output>
<output name="UnitStatus">
<field name="NAME" key="true">Unit001</field>
<field name="ITEM">UnitStatus</field>
</output>
谢谢
答案1
- Ctrl+H
- 找什么:
(<output name=").+?(?=">(?:(?!</output>).)+<field name="ITEM">(.+?)</field>)
- 用。。。来代替:
$1$2
- 查看 相符
- 查看 环绕
- 查看 正则表达式
- 查看
. matches newline
- Replace all
解释:
(<output name=") # literally, group 1
.+? # 1 or more any character, not greedy
(?= # positive lookahead, make sure we have after:
"> # literally
(?: # non capture group, Tempered Greedy Token
(?!</output>) # negative lookahead, make sure we haven't <:output> after
. # any character
)+ # end group, must appear 1 or more times
<field name="ITEM"> # literally
(.+?) # group 2, 1 or more any character, not greedy
</field> # literally
) # end lookahead
替代品:
$1 # content of group 1 (<output name=")
$2 # content of group 2 (the value of ITEM)
截图(之前):
截图(之后):