【问题标题】:How to replace all lines based on previous lines in Notepad++?如何根据 Notepad++ 中的前几行替换所有行?
【发布时间】:2017-02-01 19:09:03
【问题描述】:

我有一个 XML 代码:

<Line1>Matched_text Other_text</Line1>
<Line2>Text_to_replace</Line2>

如何告诉 Notepad++ 找到Matched_text 并将Text_to_replace 替换为Replaced_text?有几个相似的代码块,一个正好是Matched _text,另一个是Other_textText_to_replace。我想一次性全部替换。

我的想法是放

Matched_text*<Line2>*</Line2>

在“查找”字段中,并且

Matched_text*<Line2>Replaced_text</Line2>    

在替换字段中。我知道正则表达式中的\1 可能有用,但我不知道从哪里开始。


实际代码是:

<Name>Matched_text, Other_text</Name>
<IsBillable>false</IsBillable>
<Color>-Text_to_replace</Color>

【问题讨论】:

    标签: regex xml replace notepad++


    【解决方案1】:

    您要查找的正则表达式类似于以下内容。

    查找:(Matched_text[\w,\s&lt;&gt;\/]*&lt;Color&gt;-).*(&lt;/Color&gt;)

    替换:\1Replaced_text\2

    分解:

    `()` is how you tell regex that you want to keep things (for use in /1, /2, etc.), these are called capture groups in regex land.
    
    `Matched_text[\w,\s<>\/]*` means you want your anchor `Matched_text` and everything after it up till the next part of the expression.
    
    `<Color>-).*(</Color>)` Select everything between <Color>- and </Color> for replacement.
    

    如果您对表达式有任何疑问,我强烈建议您查看regex cheatsheet

    【讨论】:

    • 对不起,我忘了提到 Text_to_replace 在每个块中都不同。有没有办法一次性全部换掉?
    • 更新了我的答案,但如果没有您正在使用的实际示例,我只能如此准确。所以希望上面的更新行对你有用。
    • 我也更新了我的问题中的实际代码。我也尝试使用示例代码测试示例解决方案,但找不到。我用的是最新版的记事本++
    猜你喜欢
    • 2016-04-21
    • 2013-07-24
    • 2021-10-09
    • 1970-01-01
    • 2017-01-27
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    相关资源
    最近更新 更多