Matches brackets and returns a Dictionary<int,int> of matching pair indexes. Easier brace colorization & navigation.
$ dotnet add package MatchBracketsSimple, fast bracket matching utilities for .NET 6 and .NET 8. Finds matching pairs, validates balance, computes nesting depth, and supports multiple bracket types.
MatchBracketsBracketsNotes:
null when brackets are not balanced. GetPairsMultiple returns null on mismatch, but returns an empty dictionary for null/empty input.Editor/IDE features
Validation and diagnostics
Parsing and transformations
Single bracket type (defaults to ( and )):
Dictionary<int,int>? GetPairs(string text, char openingnBracket = '(', char closingBracket = ')')
null if unbalanced.bool IsBalanced(string input, char openingnBracket = '(', char closingBracket = ')')
true for balanced, false otherwise.New in 1.1.0:
int[]? GetDepthMap(string text, char openingBracket = '(', char closingBracket = ')')
-1 for non-bracket chars; null if unbalanced.int FindMatchingIndex(string text, int position, char openingBracket = '(', char closingBracket = ')')
position, or -1 if invalid/unbalanced.List<(int openIndex, int closeIndex, int depth)>? GetPairsWithDepth(string text, char openingBracket = '(', char closingBracket = ')')
null if unbalanced.(int index, char character, string reason)? GetFirstMismatch(string text, char openingBracket = '(', char closingBracket = ')')
null if balanced.Dictionary<int,int>? GetPairsMultiple(string text, params (char open, char close)[] bracketTypes)
() { } [ ] < >). Returns null on mismatch/imbalance; empty dictionary for null/empty input.GetPairs, GetDepthMap, GetPairsWithDepth → nullFindMatchingIndex → -1GetPairsMultiple → null (unless the input is null/empty → returns empty dictionary)IsBalanced → trueGetPairsMultiple → empty dictionary