Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

c# - 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

For example

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

It should a function which takes 3 parameters

parameter 1 : the text

parameter 2 : the beginning part of string pair e.g. <!--

parameter 3 : the end part of string pair e.g. -->

Using latest .net framework 4.8+

edit

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

such as

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

will become

    ok like readable English. Many desktop publishing packages


     Lorem Ipsum as their de

here example in code

enter image description here

here samples. sample 4 currently not working

https://dotnetfiddle.net/mA3waq

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

Sample Input

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

Output

packages  Lorem

Sample


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...