fork(3) download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class Example
  5. {
  6. public static void Main()
  7. {
  8. var re = @"(?x) # ignore spaces and comments
  9. (?=
  10. (
  11. \( # first (
  12. (?:
  13. (?<open> \( )* # open++
  14. [^()]+
  15. (?<-open> \) )* # open--
  16. )+
  17. \) # last )
  18. (?(open)(?!)) # fail if unblanaced: open > 0
  19. )
  20. )
  21. \( # eat a (, to advance the match a char";
  22.  
  23. var str = "a + ((b + (c + d)) + (e + f)) + (x + ((y) + (z)) + x)";
  24.  
  25. var m = Regex.Matches(str, re);
  26.  
  27. Console.WriteLine("Matched: ");
  28. foreach (Match i in m)
  29. Console.WriteLine(i.Groups[1]);
  30. }
  31. }
Success #stdin #stdout 0.07s 34104KB
stdin
Standard input is empty
stdout
Matched: 
((b + (c + d)) + (e + f))
(b + (c + d))
(c + d)
(e + f)
(x + ((y) + (z)) + x)
((y) + (z))
(y)
(z)