你能告诉我在 Notepad++ 中搜索什么,以便找到下面选项代码为09的所有节点并删除该节点吗?例如,我希望能够搜索下面的 xml 并删除第一个条目并留下下面的内容。
我尝试搜索<Vehicle>.*?</Vehicle>
它可以替换空白值,但是我还想添加条件来搜索整个节点中的 09 值。是否可以为“">09<”文本字符串值添加搜索条件?
在这里搜索:
<Vehicle>
<InvoiceDateTime>2016-03-20T00:00:00</InvoiceDateTime>
<InvoiceChargeCents>63</InvoiceChargeCents>
<OptionCode>09</OptionCode>
<JobEndDateTime>2016-03-19T00:00:00</JobEndDateTime>
<AuthorizationCode />
</Vehicle>
<Vehicle>
<InvoiceDateTime>2016-03-20T00:00:00</InvoiceDateTime>
<InvoiceChargeCents>63</InvoiceChargeCents>
<OptionCode>35</OptionCode>
<JobEndDateTime>2016-03-19T00:00:00</JobEndDateTime>
<AuthorizationCode />
</Vehicle>
返回以下条目:
<Vehicle>
<InvoiceDateTime>2016-03-20T00:00:00</InvoiceDateTime>
<InvoiceChargeCents>63</InvoiceChargeCents>
<OptionCode>35</OptionCode>
<JobEndDateTime>2016-03-19T00:00:00</JobEndDateTime>
<AuthorizationCode />
</Vehicle>
最佳答案
我的方法包括匹配 <Vehicle>
打开节点,然后限制点匹配,使其既不能匹配打开节点,也不能匹配关闭节点 Vehicle
具有 tempered greedy token 的节点:
<Vehicle>(?:(?!</?Vehicle>).)*>09<.*?</Vehicle>\R*
.
匹配换行符选项应该启用。
请参阅regex demo ,替换为空字符串。
(?:(?!</?Vehicle>).)*
是一个调和的贪婪标记,匹配第一个 >09<
之前的任何文本在最近的<Vehicle>
之后打开节点。
请注意\R*
匹配零个或多个换行序列(CRLF、CR 或 LF)。
另请注意,更有效的模式将是上述模式的展开版本:
<Vehicle>[^<]*(?:<(?!\/?Vehicle>)[^<]*)*>09<.*?<\/Vehicle>\R*
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
它匹配相同的文本,但对于较大的文本更有效。
关于regex - notepad++ xml节点正则表达式查找和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226123/