fork download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var inputString = "car[brand=saab][wheels=4]";
  10. var lst=Regex.Matches(inputString, @"(\w+)((?:\[.*?\])+)").Cast<Match>()
  11. .Select(x=>new
  12. {
  13. name=x.Groups[1].Value,
  14. value=Regex.Matches(x.Groups[2].Value,"\\[.*?\\]").Cast<Match>()
  15. .Select(y=>new
  16. {
  17. name= y.Groups[0].Value.Split('=')[0],
  18. value= y.Groups[0].Value.Split('=')[1]
  19. })
  20. });
  21.  
  22. foreach(var parent in lst)
  23. {
  24. Console.WriteLine(parent.name);
  25. foreach(var pairs in parent.value)
  26. {
  27. Console.WriteLine(pairs.name);
  28. Console.WriteLine(pairs.value);
  29. }
  30. }
  31. }
  32. }
Success #stdin #stdout 0.08s 34824KB
stdin
Standard input is empty
stdout
car
[brand
saab]
[wheels
4]