simonradev

QueryMess

Dec 7th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 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.     class QueryMess
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Dictionary<int, Dictionary<string, List<string>>> info =
  15.                 new Dictionary<int, Dictionary<string, List<string>>>();
  16.  
  17.             string inputLine = Console.ReadLine();
  18.             int dicCounter = 0;
  19.             while (!inputLine.Equals("END"))
  20.             {
  21.                 info[dicCounter] = new Dictionary<string, List<string>>();
  22.  
  23.                 string[] splittedInput = inputLine.
  24.                     Split(new char[] { '&' , '?'}, StringSplitOptions.RemoveEmptyEntries).ToArray();
  25.  
  26.                 string key = string.Empty;
  27.                 string value = string.Empty;
  28.                 foreach (string input in splittedInput)
  29.                 {
  30.                     if (Regex.IsMatch(input, @"(.*?)=(.*)"))
  31.                     {
  32.                         Match match = Regex.Match(input, @"(.*?)=(.*)");
  33.                         key = match.Groups[1].ToString();
  34.                         value = match.Groups[2].ToString();
  35.  
  36.                         key = Regex.Replace(key, @"((%20*\+*)|(\+))+", "");
  37.                         key = Regex.Replace(key, @"\s*", "");
  38.  
  39.                         value = Regex.Replace(value, @"((%20*\+*)|(\+))+", " ");
  40.                         value = Regex.Replace(value, @"\s+", " ");
  41.                         value = value.Trim();
  42.  
  43.                         if (!info[dicCounter].ContainsKey(key))
  44.                         {
  45.                             info[dicCounter].Add(key, new List<string>());
  46.                             info[dicCounter][key].Add(value);
  47.                         }
  48.                         else
  49.                         {
  50.                             info[dicCounter][key].Add(value);
  51.                         }
  52.                     }
  53.                 }
  54.                
  55.                 inputLine = Console.ReadLine();
  56.                 dicCounter++;
  57.             }
  58.  
  59.             foreach (int currLine in info.Keys)
  60.             {
  61.                 foreach (KeyValuePair<string, List<string>> pair in info[currLine])
  62.                 {
  63.                     Console.Write($"{pair.Key}=[{string.Join(", ", pair.Value)}]");
  64.                 }
  65.                 Console.WriteLine();
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment