Advertisement
riff-raff

02. Match phone numbers

Jun 24th, 2018
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _02._Match_Phone_Number
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string pattern = @"\+([359]+)([- ])2(\2)(\d{3})(\2)(\d{4})\b";
  12.             string phonesInputs = Console.ReadLine();
  13.             Regex phonePatternex = new Regex(pattern);
  14.             var match = phonePatternex.Matches(phonesInputs);
  15.             int phoneNumbersCount = 0;
  16.  
  17.             foreach (var phoneNumber in match)
  18.             {
  19.                 string collection = phoneNumber + ", ";
  20.                 phoneNumbersCount += 1;
  21.                 if (phoneNumbersCount == match.Count)
  22.                 {
  23.                     Console.Write(collection.Remove(collection.Length - 2));
  24.                 }
  25.                 else
  26.                 {
  27.                     Console.Write(collection);
  28.                 }
  29.             }
  30.             Console.WriteLine();
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement