Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- while (input != "END")
- {
- StringBuilder build = RemoveSpaces(input);
- Dictionary<string, List<string>> data = GetCurrentFields(build.ToString());
- PrintCurrentResult(data);
- input = Console.ReadLine();
- }
- }
- static void PrintCurrentResult(Dictionary<string, List<string>> data)
- {
- foreach (var field in data)
- {
- Console.Write($"{field.Key}=[" + string.Join(", ", field.Value) + "]");
- }
- Console.WriteLine();
- }
- static Dictionary<string, List<string>> GetCurrentFields(string input)
- {
- Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
- MatchCollection matches = Regex.Matches(input, @"([^&=?]+)=([^&=?]+)");
- foreach (Match m in matches)
- {
- string field = m.Groups[1].Value.Trim();
- string value = m.Groups[2].Value.Trim();
- if (data.ContainsKey(field) == false)
- {
- data.Add(field, new List<string>());
- }
- data[field].Add(value);
- }
- return data;
- }
- static StringBuilder RemoveSpaces(string input)
- {
- StringBuilder build = new StringBuilder().Append(input);
- MatchCollection muchSpaces = Regex.Matches(input, @"(\+|%20| ){2,}");
- foreach (Match m in muchSpaces)
- {
- build.Replace(m.ToString(), " ");
- }
- MatchCollection space1 = Regex.Matches(build.ToString(), @"%20");
- MatchCollection space2 = Regex.Matches(build.ToString(), @"\+");
- for (int r = 0; r < space1.Count; r++)
- {
- build.Remove(build.ToString().IndexOf("%20"), 3);
- }
- for (int r = 0; r < space2.Count; r++)
- {
- build.Remove(build.ToString().IndexOf("+"), 1);
- }
- return build;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement