Advertisement
Filkolev

Phone Numbers

May 27th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class PhoneNumbers
  6. {
  7.     public static void Main()
  8.     {
  9.         StringBuilder sb = new StringBuilder();
  10.  
  11.         while (true)
  12.         {
  13.             string input = Console.ReadLine();
  14.  
  15.             if (input == "END")
  16.             {
  17.                 break;
  18.             }
  19.  
  20.             sb.Append(input);
  21.         }
  22.  
  23.         const string pattern = @"(?<name>[A-Z][a-zA-Z]*)[^a-zA-Z+]*?(?<phone>\+?[0-9][0-9.() \-\/]*[0-9])";
  24.  
  25.         Regex regex = new Regex(pattern);
  26.  
  27.         var matches = regex.Matches(sb.ToString());
  28.  
  29.         if (matches.Count == 0)
  30.         {
  31.             Console.WriteLine("<p>No matches!</p>");
  32.             return;
  33.         }
  34.  
  35.         Console.Write("<ol>");
  36.         foreach (Match match in matches)
  37.         {
  38.             string phone = Regex.Replace(match.Groups["phone"].Value, @"[^+0-9]+", string.Empty);
  39.            
  40.             Console.Write(
  41.                 "<li><b>{0}:</b> {1}</li>",
  42.                 match.Groups["name"],
  43.                 phone);
  44.         }
  45.  
  46.         Console.Write("</ol>");
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement