Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove text between multiple pairs of brackets?

Tags:

c#

regex

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!

like image 511
John Wakefield Avatar asked Feb 25 '17 18:02

John Wakefield


People also ask

How do I get rid of text between parentheses in Python?

If you want to remove the [] and the () you can use this code: >>> import re >>> x = "This is a sentence. (once a day) [twice a day]" >>> re.

How do you remove content inside brackets without removing brackets in Python?

Method 1: We will use sub() method of re library (regular expressions). sub(): The functionality of sub() method is that it will find the specific pattern and replace it with some string. This method will find the substring which is present in the brackets or parenthesis and replace it with empty brackets.


2 Answers

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,}", " ");
    
like image 102
Dmitry Bychenko Avatar answered Sep 18 '22 21:09

Dmitry Bychenko


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);
like image 33
Surya-AIS Avatar answered Sep 19 '22 21:09

Surya-AIS