Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...