如何从文本中删除任何给定的字符串对

不幸的是我的正则表达式技能很差

我想编写一个函数,该函数可以删除任何给定的字符串对以及它们之间的任何内容

例如

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters,as op<!--posed to using--> 'Content here,content here',making it look like readable English. Many desktop publishing packages <!--and web page<!-- asdasasdas--> editors now use--> Lorem Ipsum as their default model text,and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years,sometimes by accident,sometimes on purpose (injected humour and the like).

从以上示例文本中,我想删除这些字符串对以及其中的任何内容<!-- -->

删除后,示例文本如下所示

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters,as op 'Content here,making it look like readable English. Many desktop publishing packages  Lorem Ipsum as their default model text,sometimes on purpose (injected humour and the like).

此任务有功能吗?我不想为此使用特定的正则表达式

它应该是一个带有3个参数的函数

参数1:文本

参数2:字符串对的开始部分,例如<!--

参数3:字符串对的结尾部分,例如-->

使用最新的.net Framework 4.8 +

编辑

例如链接的答案在此失败

ing packages <!--and web page<!-- asdasasdas--> editors now use--> Lorem Ipsum

此外,它还必须与多行一起工作

例如

    ok like readable English. Many desktop publishing packages
 <!--
and web page<!-- asdasasdas--> editors no
    w use--> Lorem Ipsum as their de

将成为

    ok like readable English. Many desktop publishing packages


     Lorem Ipsum as their de

此处为代码示例

如何从文本中删除任何给定的字符串对

这里是样本。示例4当前无法正常工作

https://dotnetfiddle.net/mA3waq

he85367243 回答:如何从文本中删除任何给定的字符串对

您可以在运行时中使用带有分隔符字符串的正则表达式构建。例如,

string FilterString(string source,string beginPattern,string endPattern)
{
    Regex regex = new Regex($"\\{beginPattern}.*\\{endPattern}",RegexOptions.Singleline);
    return regex.Replace(source,string.Empty);
}

样本输入

packages <!--and web page<!-- asdasasdas--> editors now use--> Lorem

输出

packages  Lorem

Sample

本文链接:https://www.f2er.com/2677591.html

大家都在问