Advertisement
simonradev

QueryMess

Feb 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 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 QueryMess
  9. {
  10.     public class QueryMess
  11.     {
  12.         public static void Main()
  13.         {
  14.             Regex validPair = new Regex(@"^(.*?=.*?)$");
  15.             Regex spaces = new Regex(@"((\+|%20)(\+|%20)*)");
  16.  
  17.             Dictionary<string, List<string>> allQueries =
  18.                                 new Dictionary<string, List<string>>();
  19.  
  20.             StringBuilder result = new StringBuilder();
  21.             string inputLine = Console.ReadLine();
  22.             while (inputLine != "END")
  23.             {
  24.                 string[] queryInfo = inputLine
  25.                     .Split(new[] { '&', '?' }, StringSplitOptions.RemoveEmptyEntries);
  26.  
  27.                 foreach (string pair in queryInfo)
  28.                 {
  29.                     if (!validPair.IsMatch(pair))
  30.                     {
  31.                         continue;
  32.                     }
  33.  
  34.                     string[] pairInfo = pair.Split('=');
  35.  
  36.                     string key = pairInfo[0];
  37.                     string value = pairInfo[1];
  38.  
  39.                     key = spaces.Replace(key, " ").Trim();
  40.                     value = spaces.Replace(value, " ").Trim();
  41.  
  42.                     if (!allQueries.ContainsKey(key))
  43.                     {
  44.                         allQueries.Add(key, new List<string>());
  45.                     }
  46.  
  47.                     allQueries[key].Add(value);
  48.                 }
  49.  
  50.                 foreach (KeyValuePair<string, List<string>> pair in allQueries)
  51.                 {
  52.                     result.Append($"{pair.Key}=[{string.Join(", ", pair.Value)}]");
  53.                 }
  54.  
  55.                 result.Append("\r\n");
  56.  
  57.                 allQueries.Clear();
  58.  
  59.                 inputLine = Console.ReadLine();
  60.             }
  61.  
  62.             Console.WriteLine(result.ToString().Trim('\r', '\n'));
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement