Tùng Kiều Vũ Thanh Tùng Kiều Vũ Thanh - 1 month ago 15
C# Question

Match text outside Parentheses

I have a string

string s = Hello, (L05-#301ME) I am a programmer.


I need to extract this to

string result = Hello, I am a programmer


That mean I need to get all text ouside the parentheses. How can I use RegEx to do this task

Answer

This should work for you:

var input = "Hello, (L05-#301ME) I am a programmer.";
var output = Regex.Replace(input, @" ?\(.*?\)", string.Empty);
Console.WriteLine(output);
// output would be "Hello, I am a programmer."
Comments