Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove string inside boundaries

I have a string that looks 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 ().

like image 419
Andrew Black Avatar asked Sep 12 '26 04:09

Andrew Black


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

like image 189
Mikael Avatar answered Sep 14 '26 16:09

Mikael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!