Advertisement
martinvalchev

Query_Mess

Feb 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 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.             string pattern = @"([^&=?\s]*)(?=\=)=(?<=\=)([^&=\s]*)";
  15.             string pattern2 = @"((%20|\+)+)";
  16.  
  17.  
  18.             string input;
  19.  
  20.             while (!((input = Console.ReadLine()) == "END"))
  21.             {
  22.                 Regex pairs = new Regex(pattern);
  23.                 MatchCollection matches = pairs.Matches(input);
  24.  
  25.  
  26.                 Dictionary<string, List<string>> results = new Dictionary<string, List<string>>();
  27.                 for (int i = 0; i < matches.Count; i++)
  28.                 {
  29.                     string field = matches[i].Groups[1].Value;
  30.                     field = Regex.Replace(field, pattern2, word => " ").Trim();
  31.  
  32.                     string value = matches[i].Groups[2].Value;
  33.                     value = Regex.Replace(value, pattern2, word => " ").Trim();
  34.  
  35.                     if (!results.ContainsKey(field))
  36.                     {
  37.                         List<string> values = new List<string>();
  38.                         values.Add(value);
  39.                         results.Add(field, values);
  40.                     }
  41.                     else if (results.ContainsKey(field))
  42.                     {
  43.                         results[field].Add(value);
  44.                     }
  45.                 }
  46.  
  47.  
  48.                 foreach (var pair in results)
  49.                 {
  50.                     Console.Write("{0}=[{1}]", pair.Key, string.Join(", ", pair.Value));
  51.                 }
  52.                 Console.WriteLine();
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement