C# Use Regex to Remove everything inside brackets and the brackets themselves


I have a string:

var schoolName = "University of Hawaii [Maui/Oahu/Kaui]";

I would like to remove everything inside the brackets and the brackets themselves so the string outputs "University of Hawaii" only.

I have been looking for the correct regex but haven't found the solution for doing this:

var pattern = @"\[(.*?)\]"; var query = "University of Hawaii [Maui/Oahu/Kaui]"; var matches = Regex.Matches(query, pattern); foreach (Match m in matches) { Console.WriteLine(m.Groups[1]); }

Thanks for your help!


You are almost there. You have to use the Replace method:

var pattern = @" \[(.*?)\]"; var query = "University of Hawaii [Maui/Oahu/Kaui]"; Console.WriteLine(Regex.Replace(query, pattern, string.Empty));


Using only \[.*?\] you can match everything including the brackets.

As I am not very familiar with C#, could you then replace it with an empty string?


You can do it like this:

var pattern = @"\s\[.*\]"; var regex = new Regex(pattern); var result = regex.Replace(query, "");


What about this:

var pattern = @"\[.*\]"; var query = "University of Hawaii [Maui/Oahu/Kaui]"; var result = Regex.Replace(query, pattern); Console.WriteLine(result);


here is the code I am using to do the job :

var pattern = @"\[(.*?)\]"; var result = Regex.Replace(stringToSanitize, pattern, string.Empty);


  • Pass by pointer/pass by reference in C
  • Copy list of file names from multiple directories
  • Read the written list of dictionaries from file in Python
  • Java Rest call with different user certs
  • Convert certificate to byte array
  • Best way for multi-language sites virtual Directories
  • Basic search in wordpress
  • How to change textview height constraint within table view cell?
  • Why context.Wait in StartAsync didn't stop the dialog
  • Why does IntStream.range(0, 100000).parallel.foreach take longer then normal for loop
  • Iterating through a folder using batch script
  • Erlang needs to connect to https server?
  • GAE python NDB projection query working in development but not in production
  • get value from xml with r by attribute
  • Why is this Animatable property being set again?
  • Velocity (VM) template request parameters: Getting GET variables
  • Create a Terminal-Based Bluetooth Monitor in XCode?
  • How exactly do you use json_decode to pass a javascript array to php?
  • Nested AJAX Calls using .done
  • if you have a DLL creating a bitmap in memory, how to return it to the browser?
  • SyntaxError: expected expression, got '.'
  • Slick: How can I combine a SQL LIKE statement with a SQL IN statement
  • How to process remote XML files with XSLT
  • JQuery Mobile Ajax Navigation in Single-Page Template
  • How to add html image in to velocity template file to send email?
  • Debugging php script timeout?
  • How to redirect into different page by user type in php and mysql
  • Separating definition/instantiation of template classes without 'extern'
  • Creating 2d platforms using JavaScript
  • openpyxl - adding new rows in excel file with merged cell existing
  • How to merge objects within array based on attribute
  • concise way of flattening multiindex columns
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Another “Cannot make static reference…” Question
  • Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
  • Cross compile glibc for arm, got undefined reference to some unwind functions