C# 要从字符串中删除的正则表达式匹配

C# 要从字符串中删除的正则表达式匹配,c#,regex,C#,Regex,我需要删除##字符串中的实例 例如 LoremImpsum##多洛=>匹配## Lorem##ASD##=>匹配## Lorem###=>不匹配 洛雷姆#=>不匹配 到目前为止,我的代码是: foreach (Match match in Regex.Matches(formattedHtml, @"(?<!#)(#!#)(?!#)") formattedHtml = formattedHtml.Replace(match.Value, ""); foreach(Regex.Ma

我需要删除
###
时,代码>字符串中的实例

例如

LoremImpsum##多洛
=>匹配
##

Lorem##ASD##
=>匹配
##

Lorem###=>不匹配

洛雷姆#=>不匹配

到目前为止,我的代码是:

foreach (Match match in Regex.Matches(formattedHtml, @"(?<!#)(#!#)(?!#)")
    formattedHtml = formattedHtml.Replace(match.Value, "");

foreach(Regex.Matches中的Match Match(formattedHtml,@)(?看起来您的代码在多次出现且仅应替换其中一次的地方失败

您的正则表达式执行了它应该执行的操作。但是,问题在于替换代码。而不是执行以下操作

foreach (Match match in Regex.Matches(formattedHtml, @"(?<!#)(#!#)(?!#)")
 formattedHtml = formattedHtml.Replace(match.Value, "");

foreach(Regex.Matches中的Match.Matches(formattedHtml,@)(?伙计,我觉得自己像个白痴。我现在明白我做错了什么了……谢谢。你的Regex
formattedHtml = Regex.Replace(formattedHtml,@"(?<!#)(#!#)(?!#)","");