Advertisement
Guest User

Untitled

a guest
May 18th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace P7_QuerryMess
  9. {
  10. class QuerryMess
  11. {
  12. static void Main(string[] args)
  13. {
  14. string[] input = Console.ReadLine().Split(new char[]{'&','?'},StringSplitOptions.RemoveEmptyEntries);
  15. string pattern = @"\s{2,}";//search 2 or more whitespaces
  16. Regex regex = new Regex(pattern);
  17. //List<string> str = new List<string>();
  18. do
  19. {
  20. for (int i = 0; i < input.Length; i++)
  21. {
  22. input[i]=input[i].Replace("%20", " ").Replace('+', ' ');
  23. input[i]=regex.Replace(input[i], " ");//reduce multiple whitespaces to single
  24. //str.Add(input[i]);
  25. }
  26. Print(input);
  27. input = Console.ReadLine().Split(new char[] { '&', '?' }, StringSplitOptions.RemoveEmptyEntries);
  28. }
  29. while (!input.Contains("END"));
  30. }
  31. static void Print(string[] input)
  32. {
  33. string key = @".+(?==)"; //new pattern to search keys
  34. Regex regKey = new Regex(key);
  35. string value = @"(?<==).+";//search values
  36. Regex regValue = new Regex(value);
  37. Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
  38. for (int i = 0; i < input.Length; i++)
  39. {
  40. Match matchKey = regKey.Match(input[i]);
  41. string currentKey = matchKey.ToString().Trim();
  42. Match matchValue = regValue.Match(input[i]);
  43. string currentValue = matchValue.ToString().Trim();
  44. if(result.Keys.Contains(currentKey)&&currentKey!=string.Empty)
  45. {
  46. result[currentKey].Add(currentValue);
  47. }
  48. else if(!result.Keys.Contains(currentKey)&&currentKey!=string.Empty)
  49. {
  50. result.Add(currentKey, new List<string>());
  51. result[currentKey].Add(currentValue);
  52. }
  53. }
  54. foreach(string item in result.Keys)
  55. {
  56. Console.Write("{0}=[{1}]", item, string.Join(", ", result[item]));
  57. }
  58. Console.WriteLine();
  59.  
  60. }
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement