我有一个包含以下数据的 XML 文件
<Shipment>
<Shipper>
<Name>Clothes</Name>
<Address1>Apparel Street</Address1>
<Country>GB</Country>
</Shipper>
<Shopper>
<Name>ABC</Name>
<Address1>Street 1</Address1>
<Country>IE</Country>
</Shopper>
</Shipment>
<Shipment>
<Shipper>
<Name>Clothes</Name>
<Address1>Apparel Street</Address1>
<Country>GB</Country>
</Shipper>
<Shopper>
<Name>XYZ</Name>
<Address1>Street 9</Address1>
<Country>US</Country>
</Shopper>
</Shipment>
我只想将购物者地址更改为特定地址,如下所示
<Shipment>
<Shipper>
<Name>Clothes</Name>
<Address1>Apparel Street</Address1>
<Country>GB</Country>
</Shipper>
<Shopper>
<Name>ABC</Name>
<Address1>Wonderland</Address1>
<Country>CA</Country>
</Shopper>
</Shipment>
<Shipment>
<Shipper>
<Name>Clothes</Name>
<Address1>Apparel Street</Address1>
<Country>GB</Country>
</Shipper>
<Shopper>
<Name>XYZ</Name>
<Address1>Wonderland</Address1>
<Country>CA</Country>
</Shopper>
</Shipment>
让我知道是否有任何简单的方法可以使用 Notepad++ 中的“查找和替换”选项来解决此问题
谢谢
答案1
- Ctrl+H
- 找什么:
(<Shopper>(?:(?!</Shopper>)[\s\S])+?<Address1>).+?(</Address1>[\s\S]+?<Country>).+?(</Country>)
- 用。。。来代替:
$1Wonderland$2CA$3
- 查看 相符
- 查看 环绕
- 查看 正则表达式
- 取消选中
. matches newline
- Replace all
解释:
( # group 1
<Shopper> # open tag
(?:(?!</Shopper>)[\s\S])+? # 1 or more any character, not greedy, and never encountered "</Shopper>"
<Address1> # open tag
) # end group 1
.+? # 1 or more any character but ewline, not greedy
( # group 2
</Address1> # close tag
[\s\S]+? # 1 or more any character, not greedy
<Country> # open tag
) # end group 2
.+? # 1 or more any character but newline
(</Country>) # group 3, end tag
替代品:
$1 # content of group 1
Wonderland # literally
$2 # content of group 2
CA # literally
$3 # content of group 3
截图(之前):
截图(之后):