John Wakefield
John Wakefield

Reputation: 527

How to remove text between multiple pairs of brackets?

I would like to remove text contained between each of multiple pairs of brackets. The code below works fine if there is only ONE pair of brackets within the string:

var text = "This (remove me) works fine!";

// Remove text between brackets.
text = Regex.Replace(text, @"\(.*\)", "");

// Remove extra spaces.
text = Regex.Replace(text, @"\s+", " ");

Console.WriteLine(text);

This works fine!

However, if there are MULTIPLE sets of brackets contained within the string too much text is removed. The Regex expression removes all text between the FIRST opening bracket and LAST closing bracket.

var text = "This is (remove me) not (remove me) a problem!";

// Remove text between brackets.
text = Regex.Replace(text, @"\(.*\)", "");

// Remove extra spaces.
text = Regex.Replace(text, @"\s+", " ");

Console.WriteLine(text);

This is a problem!

I'm stumped - I'm sure there's a simple solution, but I'm out of ideas...

Help most welcome!

Upvotes: 17

Views: 17370

Answers (2)

Surya
Surya

Reputation: 183

working example in c#, this will handle curly braces "{", so result will be.. {{pc_mem_kc}}

string str = "{{pc_mem_kc}}   of members were health (test message)";
var pattern = @"\{.*?\}}";
var data11 = Regex.Matches(str, pattern, RegexOptions.IgnoreCase);

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

You have two main possibilities:

  • change .* to .*? i.e. match as few as possible and thus match ) as early as possible:

    text = Regex.Replace(text, @"\(.*?\)", "");
    text = Regex.Replace(text, @"\s{2,}", " "); // let's exclude trivial replaces 
    
  • change .* to [^)]* i.e. match any symbols except ):

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

Upvotes: 28

Related Questions