using System; using System.Text.RegularExpressions; /* http://k...content-available-to-author-only...s.com/2010/12/14/net-regex-mathcing-mixed-balanced-parentheses/ */ public class Test { public static void Main() { string pattern = @" ( [^(){}\[\]]+ | \( (?=[^)]* (? \) ) ) | \[ (?=[^\]]* (? \] ) ) | \{ (?=[^}]* (? \} ) ) | \k (?<-Stack>) )+? (?(Stack) (?!)) "; string text = Console.ReadLine(); MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); foreach (Match match in matches) { Console.WriteLine(match.Value); } } }