Advertisement
ZhivkoPetkov

07. Query Mess

Jun 18th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 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 Query_Mess
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var consoleInput = Console.ReadLine();
  15.             string keypattern = @"([^&=?\s]*(?=\=))";
  16.             string valuepattern = @"(?<=\=)([^&=\s]*)";
  17.  
  18.             var results = new Dictionary<string, List<string>>();
  19.  
  20.             while (consoleInput != "END")
  21.             {
  22.                 var input = consoleInput.Split(new char[] { '&' }).ToList();
  23.  
  24.                 for (int i = 0; i < input.Count; i++)
  25.                 {
  26.                     Match keymatch = Regex.Match(input[i], keypattern);
  27.  
  28.                     var key = keymatch.Groups[1].Value;
  29.  
  30.                     if (keymatch.Groups[1].Value != null)
  31.                     {
  32.                         key = Regex.Replace(key, @"\+|%20", x => " ").ToString().Trim(); // skip key+, key+=>key
  33.                     }
  34.  
  35.                     if (results.ContainsKey(key) == false)
  36.                     {
  37.                        
  38.                         results.Add(key, new List<string>());
  39.                     }
  40.  
  41.  
  42.                     MatchCollection matches = Regex.Matches(input[i], valuepattern);
  43.  
  44.                     foreach (Match item in matches)
  45.                     {
  46.  
  47.                         string currentMatch = item.Groups[1].Value;
  48.  
  49.                         var splited = currentMatch.Split(new string[] { "%20", "+", "?" }, StringSplitOptions.RemoveEmptyEntries).ToList();
  50.  
  51.  
  52.                         results[key].Add(String.Join(" ", splited));
  53.  
  54.  
  55.                     }
  56.  
  57.                 }
  58.  
  59.  
  60.                 foreach (var item in results)
  61.                 {
  62.                     Console.Write($"{item.Key}=[{String.Join(", ", item.Value)}]");
  63.                 }
  64.                 Console.WriteLine();
  65.  
  66.                 results.Clear();
  67.  
  68.                 consoleInput = Console.ReadLine();
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement