-2

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

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • The linked answer does not handle inner string pairs. I have updated my question – Furkan Gözükara Jan 31 '20 at 13:06
  • The following regex should work. Be sure there's no whitespace i missed when trimming it for the comment format, but this will match nested ``; here's the regex: `).|(?<-D>))*(?(D)(?!))-->` – Zaelin Goodman Jan 31 '20 at 13:11
  • @ZaelinGoodman no i am not looking for specific solution. it has to be generic for every string pairs – Furkan Gözükara Jan 31 '20 at 13:11
  • What do you mean? How is a regex supposed to know what the string pair is if you don't specify it? What are some examples of other string pairs you want to match? This matches in your test case, so you really need to provide more information as to why if this solution doesn't work. – Zaelin Goodman Jan 31 '20 at 13:15
  • If you use String.Format on this, with {0} being the opening delimiter and {1} being the closing delimiter, it will do what you're asking. `{0}(?>(?!{0}|{1}).|{0}(?)|{1}(?<-D>))*(?(D)(?!)){1}` - Just be certain to escape whatever you format in using `Regex.Escape` if it has any special characters, or this will blow up in your face – Zaelin Goodman Jan 31 '20 at 13:18
  • @ZaelinGoodman yes certainly the text can have special characters. moreover, it should work with multi-line as well – Furkan Gözükara Jan 31 '20 at 13:22
  • If you need it to match across multiple lines, then you need to use the single line regex option so that `.` matches `\n`, or replace the `.` with `[\w\W]` - as for the special characters, `Regex.Escape` on your delimiters before formatting them into the regex will solve that problem. – Zaelin Goodman Jan 31 '20 at 13:27
  • @ZaelinGoodman thanks for answer but i really am bad with regex. i have put an image to the question can you check that and post a full answer if possible? – Furkan Gözükara Jan 31 '20 at 13:29
  • No I cannot, your question is closed as a duplicate. `string FilterString(string source, string beginpattern, string endpattern) {return Regex.Replace(source,String.Format("{0}(?>(?!{0}|{1}).|{0}(?)|{1}(?<-D>))*(?(D)(?!)){1}",Regex.Escape(beginpattern),Regex.Escape(endpattern)),String.Empty,RegexOptions.SingleLine)}` – Zaelin Goodman Jan 31 '20 at 13:35
  • @ZaelinGoodman yes it is working even with multi line. why dont you post as answer so i can mark as answer. by the way in which cases it would not work? – Furkan Gözükara Jan 31 '20 at 13:39
  • I cannot post a new answer because your question is closed by moderators as a duplicate; consider adopting my answer into the question - or, if it still allows you to self-answer (I can't remember if they still allow that for closed questions), you are free to self-answer with my function. – Zaelin Goodman Jan 31 '20 at 13:42
  • @ZaelinGoodman it seems like i can not also answer. i will try to get this question re-opened. moreover some special characters could break it you said. which are those? also i have tested 3 nested string and still worked. it should work with as many as nested string pairs right? – Furkan Gözükara Jan 31 '20 at 13:53
  • Yes, it uses .NET balancing groups to match as many nested pairs as there are in the string. Balancing groups are really interesting, I highly recommend giving them a look some time! – Zaelin Goodman Jan 31 '20 at 16:53

1 Answers1

0

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

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51