ZhivkoPetkov

REGEX - Weather

Jun 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Numerics;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace C_
  9. {
  10.  
  11.     class City
  12.     {
  13.         public string Name { get; set; }
  14.         public double Temp { get; set; }
  15.         public string Weather { get; set; }
  16.     }
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.            
  22.             var input = Console.ReadLine();
  23.             var result = new List<City>();
  24.             var pattern = @"([A-Z]{2})(\d+\.\d+)([A-Z]?[a-z]+|[a-z]?[A-Z]+)[|]";
  25.  
  26.  
  27.             while (input!="end")
  28.             {
  29.                 var cuurentinfo = input;
  30.  
  31.                 bool isMatch = Regex.IsMatch(cuurentinfo, pattern);
  32.  
  33.                 if (isMatch)
  34.                 {
  35.                     Match match = Regex.Match(cuurentinfo, pattern);
  36.                     var name = match.Groups[1].Value;
  37.                     var temperature = double.Parse(match.Groups[2].Value);
  38.                     var weather = match.Groups[3].Value;
  39.  
  40.                     var isFound = result.FirstOrDefault(x => x.Name == name);
  41.  
  42.                     if (isFound != null)
  43.                     {
  44.                         isFound.Temp = temperature;
  45.                         isFound.Weather = weather;
  46.                     }
  47.                     else
  48.                     {
  49.                         var city = new City
  50.                         {
  51.                             Name = name,
  52.                             Temp = temperature,
  53.                             Weather = weather
  54.                         };
  55.                         result.Add(city);
  56.                     }
  57.  
  58.                 }
  59.  
  60.                 input = Console.ReadLine();
  61.             }
  62.  
  63.             foreach (var city in result.OrderBy(x=>x.Temp))
  64.             {
  65.                 Console.WriteLine($"{city.Name} => {city.Temp:F2} => {city.Weather}");
  66.             }
  67.  
  68.         }
  69.     }
  70. }
Add Comment
Please, Sign In to add comment