我有一個 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)
截圖(之前):
截圖(之後):