Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. asdf
  2. [1,2]
  3. fff
  4. [4,5]
  5.  
  6. asdf1fff4
  7. asdf1fff5
  8. asdf2fff4
  9. asdf2fff5
  10.  
  11. public static IEnumerable<IEnumerable<string>> Parse(IEnumerable<string> rawData) {
  12. return
  13. rawData.Select(s =>
  14. Regex.Match(
  15. s,
  16. "^(\[((?<Item>[^,]*),)*(?<Item>[^,]*)]|(?<Item>.*))$",
  17. RegexOptions.ExplicitCapture|RegexOptions.Singleline
  18. ).Groups["Item"].Captures.Cast<Capture>().Select(c => c.Value).ToArray()
  19. );
  20. }
  21. public static IEnumerable<string> All(IEnumerable<IEnumerable<string>> parsedData) {
  22. return
  23. parsedData.DefaultIfEmpty(new[] { "Отсутствуют исходные данные." }).
  24. Aggregate((e, d) => e.SelectMany(_ => d, string.Concat));
  25. }
  26. public static void Test() {
  27. IEnumerable<string> rawData = new[] { "asdf", "[1,2]", "fff", "[4,5]" };
  28. IEnumerable<IEnumerable<string>> parsedData = Parse(rawData);
  29. IEnumerable<string> all = All(parsedData);
  30. foreach(string s in all) {
  31. Console.WriteLine(s);
  32. }
  33. }
  34.  
  35. private static string[] Generate(IEnumerable<string> rawData)
  36. {
  37. IEnumerable<IEnumerable<string>> parsedData = rawData.Select(s =>
  38. s.Trim('[').Trim(']').Split(',')
  39. );
  40.  
  41. return
  42. parsedData.DefaultIfEmpty(new[] { "Отсутствуют исходные данные." }).
  43. Aggregate((e, d) => e.SelectMany(_ => d, string.Concat)).ToArray();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement