正则表达式 - 删除模式之间的线只保留第一次出现

Regex - Remove lines between patterns keeping only first ocurrence

我需要一个 REGEX 来删除给定模式的两行之间的行,只保留下一行的第一次出现。像 uniq

这样的东西

输入:

Pattern.SomeText
RepeatedLine
RepeatedLine
RepeatedLine
Line

Pattern.OtherText
RepeatedLine

Pattern.ThirdText
RepeatedLine
TTTTRepeatedLine

输出:

Pattern.SomeText
RepeatedLine
Line

Pattern.OtherText 
RepeatedLine

Pattern.Third
TextRepeatedLine
TTTT

有模式的行总是以它开头,而且整行是唯一的。我想用Notepad++制作。

这是一个能够找到连续重复行的正则表达式示例:

const regex = /\n(.+)\n\n/g;
const str = `Pattern.SomeText
RepeatedLine
RepeatedLine
RepeatedLine
Line

Pattern.OtherText
RepeatedLine

Pattern.ThirdText
RepeatedLine
TTTT
RepeatedLine`;
const subst = `\n`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result:\n', result);

它会在不连续的重复行上失败。

以及一个简单的 C# 示例来满足确切的要求:

string pattern = "Pattern.";
string result = "";
string input = @"Pattern.SomeText
RepeatedLine
RepeatedLine
RepeatedLine
Line

Pattern.OtherText
RepeatedLine

Pattern.ThirdText
RepeatedLine
TTTT
RepeatedLine";

var a = input.Split(new string[] { pattern }, StringSplitOptions.None);
foreach (var block in a)
{
    HashSet<string> lastLines = new HashSet<string>(
        block.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
    if (lastLines.Any())
    {
        result += pattern + string.Join(Environment.NewLine, lastLines)+Environment.NewLine;
    }
}