fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. var line = "this is (a (string))";
  12. var pattern = @"\(((?>[^()]+|\((?<o>)|\)(?<-o>))*(?(o)(?!)))\)|\S+";
  13. var result = Regex.Matches(line, pattern)
  14. .Cast<Match>()
  15. .SelectMany(x => x.Groups.Cast<Group>()
  16. .Where(m => !string.IsNullOrWhiteSpace(m.Value))
  17. .Select(t => t.Value))
  18. .ToList();
  19. foreach (var s in result)
  20. Console.WriteLine(s);
  21. }
  22. }
Success #stdin #stdout 0.15s 24728KB
stdin
Standard input is empty
stdout
this
is
(a (string))
a (string)