如何从文本中删除任何给定的字符串对 [英] How to remove any given string pairs from text
问题描述
不幸的是我的正则表达式技能很差
Unfortunately my regex skills are very bad
我想编写一个函数,该函数可以删除任何给定的字符串对以及它们之间的任何内容
I would like to code a function that can remove any given pair of strings and whatever between them
例如
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).
从上面的示例文本中,我想删除这些字符串对以及其中的任何内容<!--
-->
From this above example text, i want to remove these string pairs and whatever inside them <!--
-->
删除后,示例文本如下所示
After removal the example text become as below
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, content here', making it look like readable English. Many desktop publishing packages 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).
此任务有功能吗?我不想为此使用特定的正则表达式
Are there any readily function for this task? I don't want a specific regex for this
它应该是一个带有3个参数的函数
It should a function which takes 3 parameters
参数1:文本
参数2:字符串对的开始部分,例如<!--
parameter 2 : the beginning part of string pair e.g. <!--
参数3:字符串对的结尾部分,例如-->
parameter 3 : the end part of string pair e.g. -->
使用最新的.net Framework 4.8 +
Using latest .net framework 4.8+
编辑
例如,链接的答案在此失败
the linked answer for example fails at this
ing packages <!--and web page<!-- asdasasdas--> editors now use--> Lorem Ipsum
此外,它还必须与多行一起工作
Moreover, it has to work with multi-line as well
例如
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当前无法正常工作
here samples. sample 4 currently not working
https://dotnetfiddle.net/mA3waq
推荐答案
您可以在运行时中使用带有分隔符字符串的regex构建.例如,
You could use regex build in runtime with delimiter strings. For example,
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
这篇关于如何从文本中删除任何给定的字符串对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!