How to remove text between multiple pairs of brackets?

11,121

Solution 1

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,}", " ");
    

Solution 2

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);
Share:
11,121

Related videos on Youtube

Author by

John Wakefield

Robosoup is a London based machine learning consultancy that helps organisations create value from their data. Working with your existing technology team, we create bespoke solutions and provide training to bring the next generation of smart technology to your enterprise. By automating away mundane tasks currently performed by humans, we free your workforce to focus on activities that add greater value.

Updated on June 27, 2022

Comments

  • John Wakefield 6 months

    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!

    • Dmitry Bychenko
      Dmitry Bychenko
      Try .*? instead of .* - i.e. macth as few as possible

Related