Advertisement
Guest User

Query Mess

a guest
May 25th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 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.  
  7. namespace QueryMess
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.             List<string> output = new List<string>();
  15.  
  16.             Regex regexSpaces = new Regex(@"(%20|\+)+");
  17.             string groupPattern = @"([^\?^&]+)=([^\?^&]+)";
  18.  
  19.             while (input != "END")
  20.             {
  21.                 string cleared = regexSpaces.Replace(input, " ");
  22.                 MatchCollection matches = Regex.Matches(cleared, groupPattern);
  23.                 Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
  24.                
  25.  
  26.                 foreach (Match item in matches)
  27.                 {
  28.                     List<string> list = new List<string>();
  29.                     if (! dictionary.ContainsKey(item.Groups[1].Value.Trim(' ')))
  30.                     {
  31.                         list.Add(item.Groups[2].Value.Trim(' '));
  32.                         dictionary.Add(item.Groups[1].Value.Trim(' '), list);
  33.                     }
  34.                     else
  35.                     {
  36.                         dictionary[item.Groups[1].Value.Trim(' ')].Add(item.Groups[2].Value.Trim(' '));
  37.                     }
  38.                 }
  39.                 StringBuilder stringToPrint = new StringBuilder();
  40.                 foreach (var item in dictionary)
  41.                 {
  42.                     stringToPrint.Append($"{item.Key}=[{string.Join(", ", item.Value)}]");
  43.                 }
  44.                 output.Add(stringToPrint.ToString());
  45.                 input = Console.ReadLine();
  46.             }
  47.            
  48.             for (int i = 0; i < output.Count; i++)
  49.             {
  50.                 Console.WriteLine(output[i]);
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement