简体   繁体   English

Notepad ++正则表达式查找有条件的线组

[英]Notepad++ Regex to find group of lines with condition

Given this example text: 给出以下示例文本:

<abr:rules>
<abr:ruleTypeDefinition>
<abr:code>ABB</abr:code>
<abr:ownership>
<abr:owner organization="NT" application="DCS" subapplication="FM"/>
...lines...
...........
</abr:rules>
<abr:rules>
<abr:ruleTypeDefinition>
<abr:code>ADE</abr:code>
<abr:ownership>
<abr:owner organization="NT" application="DCS" subapplication="CM"/>
...lines...
...........
</abr:rules> (end of group)

I would like to find and remove all that goes from <abr:rules> to </abr:rules> with the condition that subapplication IS NOT "CM" . 我想查找并删除从<abr:rules></abr:rules> ,条件subapplication IS NOT "CM" Organization and application are the same, <abr:code> it's any string. 组织和应用程序相同, <abr:code>是任何字符串。

What I tried so far is 我到目前为止尝试的是

<abr:rules>\n<abr:ruleTypeDefinition>\n<abr:code>[a-zA-Z0-9]{3,}<\/abr:code>\n<abr:ownership>\n<.*"(FM|PSD|SSC)"\/>\n(?s).*?\n<\/abr:rules>\n

which works but only because I know the other subapplication names. 这有效,但仅因为我知道其他子应用程序名称。

Is there any way to do it with Regex only ? 有什么办法只能使用正则表达式吗?

Try the following find and replace: 尝试以下查找并替换:

Find: 找:

<abr:rules>((?!subapplication=).)*subapplication="(?!CM")[^"]+"((?!</abr:rules>).)*</abr:rules>

Replace: 更换:

(empty string)

Demo 演示

Note: The above pattern will only work if you enable dot in Notepad++ to match newlines. 注意:只有在Notepad ++中启用点以匹配换行符时,以上模式才有效。 If you don't want to do that, then you may use [\\S\\s] instead of dot. 如果您不想这样做,则可以使用[\\S\\s]代替点。

You should not use regex for xml, you can read why here: https://stackoverflow.com/a/1732454/3763374 您不应该将正则表达式用于xml,您可以在此处阅读原因: https//stackoverflow.com/a/1732454/3763374

Instead you can use some parser like Xpath 相反,您可以使用一些解析器,例如Xpath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM