Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-02-24
3
Medium Priority
?
4,412 Views
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.
0
Comment
Question by:balajime
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 23724531

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);
              }
            mIdx++;
          }
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:balajime
ID: 31550711
Great. Got answer in a minute. Your guys are expert.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 23724648
Thanks for the question and the points.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…

564 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