fork(9) download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. /*
  5. http://k...content-available-to-author-only...s.com/2010/12/14/net-regex-mathcing-mixed-balanced-parentheses/
  6. */
  7.  
  8. public class Test
  9. {
  10. public static void Main()
  11. {
  12. string pattern = @"
  13. (
  14. [^(){}\[\]]+
  15. | \( (?=[^)]* (?<Stack> \) ) )
  16. | \[ (?=[^\]]* (?<Stack> \] ) )
  17. | \{ (?=[^}]* (?<Stack> \} ) )
  18. | \k<Stack> (?<-Stack>)
  19. )+?
  20. (?(Stack) (?!))
  21. ";
  22. string text = Console.ReadLine();
  23. MatchCollection matches = Regex.Matches(text, pattern,
  24. RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture);
  25. foreach (Match match in matches)
  26. {
  27. Console.WriteLine(match.Value);
  28. }
  29. }
  30. }
Success #stdin #stdout 0.04s 37224KB
stdin
(2)({1}2[3(4)5]6){{}{{}}[()]}
stdout
(2)
({1}2[3(4)5]6)
{{}{{}}[()]}