.NET regex to find strings inside curly braces

Regular expressions all by themselves have the magical ability to amaze and confound all at the same time. Throw in a dash of .NET string interpretation and you have a recipe for pulling out the remaining hair that you might have on your head.

So I was trying to use a regex to do some searching inside of a large string for strings inside curly braces, such as {this} and {these three words}. My string matching routine would return “this” and “these three words”, without the quotes of course. It took a bit of trial and error, but here is the important stuff for the routine that I put together to accomplish this task:

const string pattern = @"\{([^\}]+)\}";
foreach (Match match in Regex.Matches(theLargeString, pattern))
{
    Console.WriteLine("Found a field match: " + match);
}

BTW, a special birthday shout out to one of the mega stars of our generation, who turns 50 years young today. Happy birthday, Larry The Cable Guy.

Leave a Reply