alexmitev

06.Query Mess*

Dec 8th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace QueryMess
  9. {
  10.     class Program
  11.     {
  12.         static void Main()
  13.         {
  14.             var stringList = new List<string>();
  15.             Regex pattern = new Regex(@"(\w+\+?%?)=([^&]+)");
  16.             string input = Console.ReadLine();
  17.             Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
  18.             while(input != "END")
  19.             {
  20.                 input = input.Replace("?", "&");
  21.                 stringList.Add(input);
  22.                 input = Console.ReadLine();
  23.             }
  24.             for (int i = 0; i < stringList.Count; i++)
  25.             {
  26.                 MatchCollection matches = pattern.Matches(stringList[i]);
  27.                 foreach(Match match in matches)
  28.                 {
  29.                     string name = Regex.Replace(match.Groups[1].ToString(), @"\+?%20\+?|\+", " ").Trim();
  30.                     string value = Regex.Replace(match.Groups[2].ToString(), @"\+?%20\+?|\+", " ").Trim();
  31.                     if(!result.ContainsKey(name))
  32.                     {
  33.                         result[name] = new List<string>();
  34.  
  35.                     }
  36.                     result[name].Add(value);
  37.                     //Console.WriteLine(name);
  38.                     //Console.WriteLine(value);
  39.                 }
  40.               foreach(var pair in result)
  41.               {
  42.                   Console.Write("{0}=[{1}]", pair.Key, string.Join(", ", pair.Value));
  43.               }
  44.               Console.WriteLine();
  45.               result.Clear();          
  46.             }
  47.  
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment