我需要一个REGEX来删除给定模式的2行之间的行,只保留第一次出现的下一行。像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 ++制作它。
答案 0 :(得分:0)
这是一个正则表达式的例子,它能够找到连续的重复行:
const regex = /\n(.+)\n\1\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;
}
}