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!
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.
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.
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,}", " ");
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With