如何在C#Regex中使用lookbehind来删除换行符?

如何在C#Regex中使用lookbehind来删除换行符?,c#,regex,C#,Regex,我有一个以重复结构作为标题的文本文件和一个详细记录,如 StopService:: 697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to A@gmail.com::0:::: 我想删除标题和详细信息记录之间的换行符,以便将它们作为单个记录处理,因为详细信息记录也可以包含换行符,我只需要删除紧跟:符号的换行符 我不擅长使用正则表达式,所以我搜索并尝试使用这种方法,

我有一个以重复结构作为标题的文本文件和一个详细记录,如

StopService::
697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to
A@gmail.com::0::::
我想删除标题和详细信息记录之间的换行符,以便将它们作为单个记录处理,因为详细信息记录也可以包含换行符,我只需要删除紧跟
符号的换行符

我不擅长使用正则表达式,所以我搜索并尝试使用这种方法,但不起作用:

 string text = File.ReadAllText(path);
 Regex.Replace(text, @"(?<=(:))(?!\1):\n", String.Empty);
 File.WriteAllText(path, text);
Javascript:

yourtext.replace(/(\r\n|\n|\r)/gm," ");
我还没有测试过C#one。它应该像下面这样工作

C#:

试试这个:

Regex.Replace(你的文本,@)(?非Regex方式
逐行读取文件。检查第一行是否等于
StopService::
不要在其后面添加换行符(
Environment.newline


正则表达式方式
您可以使用
(?这是我的快速尝试。它可能需要一些调整,因为我刚刚模拟了两条记录以供输入

方法是定义一个正则表达式来标识标题、换行符和细节(可能包括换行符)。然后,只需运行一个替换,将标题与细节放回一起,抛出标题/细节换行符

RegexOptions.IgnorePatternWhitespace选项用于在表达式中允许空白,以提高可读性

var text = "StopService::" + Environment.NewLine;
text += "697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to" + Environment.NewLine;
text += "A@gmail.com::0::::"  + Environment.NewLine;
text += "StopService::" + Environment.NewLine;
text += "697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to" + Environment.NewLine;
text += "A@gmail.com::0::::"  + Environment.NewLine;

var options = RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace;
var matchRegex = new Regex("(?<header>\\w+?::) \\r\\n (?<detail>.+?::::)", options );
var replacement = "${header}${detail}";

var newText = matchRegex.Replace(text,replacement);

多棒的正则表达式。您捕获了一个冒号,然后在一个前瞻中使用对它的反向引用,以确保下一个字符不是冒号,然后是冒号。难怪它不起作用。请尝试
(?谢谢,它也不起作用。我相信没有任何东西会起作用,因为我们不知道从您的描述中必须如何工作。为您给定的字符串提供预期的输出。不清楚的问题往往会得到不清楚的答案,已经有一个了。我添加了我想要的结果,我只需要在
停止服务:
和t的其余部分之间换行要删除的记录“m”代表多行匹配。虽然更多空间用于正则表达式解决方案,但我会使用非正则表达式解决方案。由您决定。
yourtext.replace(/(\r\n|\n|\r)/gm," ");
Regex.Replace(yourtext, @"/(\r\n|\n|\r)/gm", " ");
var str = "StopService::\r\n697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to\r\nA@gmail.com::0::::";
var rgx = new Regex(@"(?<=^[^:]*::)[\r\n]+");
Console.WriteLine(rgx.Replace(str, string.Empty));
StopService::697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to
A@gmail.com::0::::
var text = "StopService::" + Environment.NewLine;
text += "697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to" + Environment.NewLine;
text += "A@gmail.com::0::::"  + Environment.NewLine;
text += "StopService::" + Environment.NewLine;
text += "697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to" + Environment.NewLine;
text += "A@gmail.com::0::::"  + Environment.NewLine;

var options = RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace;
var matchRegex = new Regex("(?<header>\\w+?::) \\r\\n (?<detail>.+?::::)", options );
var replacement = "${header}${detail}";

var newText = matchRegex.Replace(text,replacement);
StopService::697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to
A@gmail.com::0::::
StopService::697::12::test::20::a@yahoo.com::20 Main Rd::Alcatraz::CA::1200::Please send me Information to
A@gmail.com::0::::