[ACCEPTED]-How to remove text in brackets using a regular expression-regex

Accepted answer
Score: 43

This should do the job:

var input = "User Name (email@address.com)";
var output = Regex.Replace(input, @" ?\(.*?\)", string.Empty);

Note the escaping 2 of the ( and ) chars so that they aren't recognised 1 as group markers.

Score: 9

Did you mean you want the user name output, rather 7 than the email address? For either case, you 6 don't need regexes. For example, assuming the 5 input is always well-formed and hence leaving 4 out any error checking,

string output = input.Substring(0, input.IndexOf(" ("))

will get you the 3 user name. And if you did want the email 2 address, that too is available without resorting 1 to regexes:

int n;
string output = input.Substring(n = 1 + input.IndexOf('('),
                                input.IndexOf(')') - n)
Score: 0

I'm just offering another way to do this, although 2 I would just use regex myself as this is 1 clunky:

    string output = input.Split('(')[0].TrimEnd();

More Related questions