在Notepad ++中使用正则表达式,如何在匹配表达式后捕获上一行

时间:2017-04-19 18:57:33

标签: regex notepad++ lookahead lookbehind

我有一个包含数千个条目的文件:

<Source foo="goo">
    <Name label="SomeLabel"/>
</Source>
<Target foo="bar">
    <Name label="SomeLabel"/>
</Target>

唯一改变的是&#34; SomeLabel&#34;串。我试图在Notepad ++中编写一个搜索表达式,它只返回&#39; SomeLabel&#39;字符串,或至少整行。但是,我想要该行,如果它在&lt; Target&gt;之间。标签;我不想要&lt; Source&gt;节点。我知道我需要使用lookbehind(或lookahead),但我无法弄明白,特别是使用&lt;&gt;要匹配的字符串中的字符。

谢谢!

2 个答案:

答案 0 :(得分:0)

&lt;&gt;不是真的有问题。在这些限制下,您可以轻松地使用

获取所需的标签
[^"]+(?="\/>[^<]*<\/Target)

答案 1 :(得分:0)

您可以使用以下表达式

<Target[^>]*>             # match <Target...>
(?:(?!</Target>)[\s\S])*? # match anything afterwards, 
                          # make sure not to overrun </Target>
label="SomeLabel"         # match label="SomeLabel"
(?:(?!</Target>)[\s\S])*? # same as above
</Target>                 # closing Tag

此处,仅选择label="SomeLabel"条目,使用详细模式并查看a demo on regex101.com

相关问题