Link Search Menu Expand Document

How to Find Brackets with Regex

This article shows the use of C# regular expressions (REGEX) for finding different types of brackets within a C# string.

In this article, you will see the following use cases of finding brackets within C# strings.

  1. How to find a single bracket type
  2. How to find multiple bracket types
  3. How to find text along with brackets
  4. How to find text inside brackets

Finding a Single Bracket

To find a single bracket within a C# string, you can use the Matches() function. You need to pass the input string and the regex expression that searches for a specific bracket to the Matches() method.

For instance, if you want to search for a square bracket, you need to pass “[” as the regex expression. Here the backslash “\” will tell the regex compiler to escape the square brackets and treat it as an ordinary character. By default, a square bracket is treated as a special character used to group characters in regex expressions in C#.

The following script looks for a single square bracket inside the input string.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Your name is [John] and your age is (32). You are {married}";

            string regex = @"\[";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of brackets found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Bracket " + m.Value + " found at index: " + m.Index);
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

 

In the output, you can see the index of the matched square bracket.

Output:

Regular Expressions Find Brackets

 

Finding Multiple Brackets Types

Using the regex expression, you can find square brackets, round brackets “()”, and curly brackets “{}” inside a C# string. To do so, you can use the Matches() method. However, this time the regex expression will be “[[](){}]”.

In the aforementioned regex expression,  the outer square brackets specify that any of the bracket types mentioned inside should be matched. Inside the square brackets, you use backward slash “\” to escape different bracket types e.g. square brackets “[]”, parenthesis “()”, and braces “{}”.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Your name is [John] and your age is (32). You are {married}";

            string regex = @"[\[\]\(\)\{\}]";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of brackets found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Bracket " + m.Value + " found at index: " + m.Index);
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

 

In the output below, you can see the indexes for the different bracket types in our input string.

Output:

Regular Expressions C# Find Brackets

 

Finding Text Including Brackets

A common use case for string parsing is the extraction of text within brackets. The regex Matches() method can be used for this purpose as well. For instance, to search for the text inside the square brackets “[]”, you can use the regex expression “[(.*?)]”.

The following script returns the text from inside the square brackets.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Your name is [John] and your age is (32). You are {married}";

            string regex = @"\[(.*?)\]";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of brackets found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Values " + i + " with brackets: " + m.Value);
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

In the output below, you can see the text inside the square brackets.

Output:

Reg Exp Find Brackets

 

In the same way, you can search text within different types of brackets. You only have to change the regex expression which in this case will be “[({[])}]”. Here is an example of searching text within different types of brackets.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Your name is [John] and your age is (32). You are {married}";

            string regex = @"[\[\(\{](.*?)[\]\)\}]";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of brackets found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Values " + i + " with brackets: " + m.Value);
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

 

Output:

Reg Ex Find Brackets

 

Finding Text Only Inside Brackets

In the previous section, you saw how to find text within different types of brackets. However, in the previous section, the brackets were also returned by the Matches() method. What if you only want the text within brackets and not the brackets themselves?

You can do so using the Groups attribute of the Match objects that are returned as collection by the Matches() method. In the following script, the first index of the Groups collection contains text only.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Your name is [John] and your age is (32). You are {married}";

            string regex = @"[\[\(\{](.*?)[\]\)\}]";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of brackets found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Values " + i + " with brackets: " + m.Groups[1].Value);
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

 

In the output below, you can see the text-only that occurs inside brackets in our input strings.

Output:

How to Find Brackets with Regex

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy