Advertisement
vasildiavolo

07. Query Mess

Jun 18th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string input = Console.ReadLine();
  11.  
  12. while (input != "END")
  13. {
  14. StringBuilder build = RemoveSpaces(input);
  15. Dictionary<string, List<string>> data = GetCurrentFields(build.ToString());
  16. PrintCurrentResult(data);
  17.  
  18. input = Console.ReadLine();
  19. }
  20. }
  21.  
  22. static void PrintCurrentResult(Dictionary<string, List<string>> data)
  23. {
  24. foreach (var field in data)
  25. {
  26. Console.Write($"{field.Key}=[" + string.Join(", ", field.Value) + "]");
  27. }
  28.  
  29. Console.WriteLine();
  30. }
  31.  
  32. static Dictionary<string, List<string>> GetCurrentFields(string input)
  33. {
  34. Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
  35. MatchCollection matches = Regex.Matches(input, @"([^&=?]+)=([^&=?]+)");
  36.  
  37. foreach (Match m in matches)
  38. {
  39. string field = m.Groups[1].Value.Trim();
  40. string value = m.Groups[2].Value.Trim();
  41.  
  42. if (data.ContainsKey(field) == false)
  43. {
  44. data.Add(field, new List<string>());
  45. }
  46. data[field].Add(value);
  47. }
  48.  
  49. return data;
  50. }
  51.  
  52. static StringBuilder RemoveSpaces(string input)
  53. {
  54. StringBuilder build = new StringBuilder().Append(input);
  55.  
  56. MatchCollection muchSpaces = Regex.Matches(input, @"(\+|%20| ){2,}");
  57. foreach (Match m in muchSpaces)
  58. {
  59. build.Replace(m.ToString(), " ");
  60. }
  61.  
  62. MatchCollection space1 = Regex.Matches(build.ToString(), @"%20");
  63. MatchCollection space2 = Regex.Matches(build.ToString(), @"\+");
  64.  
  65. for (int r = 0; r < space1.Count; r++)
  66. {
  67. build.Remove(build.ToString().IndexOf("%20"), 3);
  68. }
  69. for (int r = 0; r < space2.Count; r++)
  70. {
  71. build.Remove(build.ToString().IndexOf("+"), 1);
  72. }
  73.  
  74. return build;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement