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.
- How to find a single bracket type
- How to find multiple bracket types
- How to find text along with brackets
- 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:
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:
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:
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:
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:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C
- How to Split Text Using Regex
- How to Find HTML Tags Using Regex
- How to Validate Email Address via Regex in C#
- How to Extract Amount with Currency Symbols using Regex in C#
- How to Find Brackets with Regex