how to find strings between curly brackets (using C# Regex split)

Posted on 2009-02-24
Last Modified: 2013-12-17
I want to find strings between curly brackets (using C# Regex split)

sample string:
This is a test string {First curly bracket} This is a test string {Second curly bracket}

output i need in string array:
{First curly bracket}
{Second curly bracket}

Any help would be very useful. Please give code sample.
Question by:balajime
    LVL 27

    Accepted Solution


    using System;
    using System.Text.RegularExpressions;
    namespace myapp
      class Class1
          static void Main(string[] args)
              String sourcestring = "source string to match with pattern";
              Regex re = new Regex(@"\{[^}]*\}");
              MatchCollection mc = re.Matches(sourcestring);
              int mIdx=0;
              foreach (Match m in mc)
                for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
                    Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);

    Open in new window


    Author Closing Comment

    Great. Got answer in a minute. Your guys are expert.
    LVL 27

    Expert Comment

    Thanks for the question and the points.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now