C# remove string inside bounderies

Viewed 45

sorry if this is duplicate or something but i was unable to find solution in c# for my problem. I have c# string that lookl like this:

string aa = "First Example (first to delete) , Second example ( Second to delete ) , Thrird Example ( Third to delete )"

From this string I want to delete everything that is inside ( SOME INPUT ) with ( and ).

Problem is that I dont know what this SOME INPUT is. I only know that it is inside ().

Thanks for any help.

1 Answers

https://dotnetfiddle.net/Jnhszs

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
    var text = "First Example (first to delete) , Second example ( Second to delete ) , Thrird Example ( Third to delete )";

    text = Regex.Replace(text, @"\([^)]*\)", "");
    text = Regex.Replace(text, @"\s{2,}", " ");

    Console.WriteLine(text);
    }
}

Output: First Example , Second example , Thrird Example

Related