Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace _10b_Regular_Expressions_RegEx_Exercises
- {
- class Regular_Expressions_RegEx_Exercises
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- string pattern = @"(?<city>[A-Z]{2})(?<temperature>([\d]+)\.([\d]+))(?<weather>[A-Za-z]+)(\|)";
- List<string> lines = new List<string>();
- while (input != "end")
- {
- lines.Add(input);
- input = Console.ReadLine();
- }
- List<Match> myMatches = new List<Match>();
- foreach (var line in lines)
- {
- MatchCollection m = Regex.Matches(line, pattern);
- foreach (Match thisMatch in m)
- {
- myMatches.Add(thisMatch);
- }
- }
- Dictionary<string, List<string>> weather = new Dictionary<string, List<string>>();
- foreach (Match current in myMatches)
- {
- if (!weather.ContainsKey(current.Groups["city"].Value))
- {
- List<string> list = new List<string>();
- list.Add(current.Groups["temperature"].Value);
- list.Add(current.Groups["weather"].Value);
- weather.Add(current.Groups["city"].Value, list);
- }
- else
- {
- weather[current.Groups["city"].Value].Clear();
- weather[current.Groups["city"].Value].Add(current.Groups["temperature"].Value);
- weather[current.Groups["city"].Value].Add(current.Groups["weather"].Value);
- }
- }
- foreach (var pair in weather.OrderBy(x => double.Parse(x.Value[0])))
- {
- Console.WriteLine($"{pair.Key} => {double.Parse(pair.Value[0]):F2} => {pair.Value[1]}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment