Advertisement
dimipan80

Query Mess

May 13th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. /* The input is passed as array of query strings. For each field of the form, the query string contains a pair field=value. Within each pair, the field name and value are separated by an equals sign, '='. The series of pairs are separated by the ampersand, '&’. The question mark is used as a separator and is not part of the query string. SPACE is encoded as '+' or "%20". URL query string may contain another URL as value. Print at single line each processed string as follows:  key=[value]nextkey=[another  value] … etc. Multiple whitespace characters should be reduced to one inside value/key names, but there shouldn’t be any whitespaces before/after extracted keys and values. If a key already exists, the value is added with comma and whitespace after other values of the existing key in current string. */
  2.  
  3. namespace _07.QueryMess
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.     using System.Text;
  8.     using System.Text.RegularExpressions;
  9.  
  10.     class QueryMess
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Queue<string> linkStrings = new Queue<string>();
  15.             StringBuilder inputLine = new StringBuilder(Console.ReadLine());
  16.             while (inputLine.ToString() != "END")
  17.             {
  18.                 linkStrings.Enqueue(inputLine.ToString());
  19.                 inputLine.Clear();
  20.                 inputLine.Append(Console.ReadLine());
  21.             }
  22.  
  23.             foreach (string link in linkStrings)
  24.             {
  25.                 GetPairsFromLinkString(link);
  26.             }
  27.  
  28.         }
  29.  
  30.         private static void GetPairsFromLinkString(string link)
  31.         {
  32.             Dictionary<string, Queue<string>> pairs = new Dictionary<string, Queue<string>>();
  33.             const string pattern = @"[^=&?\s]+=[^=&?\s]+";
  34.             MatchCollection matches = Regex.Matches(link, pattern);
  35.             foreach (Match match in matches)
  36.             {
  37.                 Regex rgx = new Regex(@"(\+|%20)+");
  38.                 string pair = rgx.Replace(match.ToString(), " ");
  39.                 string[] fieldNames = pair.Split('=');
  40.                 string key = fieldNames[0].Trim();
  41.                 string value = fieldNames[1].Trim();
  42.  
  43.                 if (!pairs.ContainsKey(key))
  44.                 {
  45.                     pairs[key] = new Queue<string>();
  46.                 }
  47.  
  48.                 pairs[key].Enqueue(value);
  49.             }
  50.  
  51.             PrintResultString(pairs);
  52.         }
  53.  
  54.         private static void PrintResultString(Dictionary<string, Queue<string>> pairs)
  55.         {
  56.             StringBuilder result = new StringBuilder();
  57.             foreach (var entry in pairs)
  58.             {
  59.                 result.Append(entry.Key).Append("=[");
  60.                 result.Append(string.Join(", ", entry.Value)).Append("]");
  61.             }
  62.  
  63.             Console.WriteLine(result);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement