Advertisement
Filkolev

Query Mess

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