Вывести все номера телефонов, содержащиеся в сообщении соответсвующие форматам xx-xx-xx, xxx-xxx и xxx-xx-xx - C#
Формулировка задачи:
Вывести все номера телефонов, содержащиеся в сообщении соответсвующие форматам xx-xx-xx, xxx-xxx и xxx-xx-xx
Даже и не знаю как вывести их. Прошу о помощи.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- string tel1 = @"\d{3}-\d{2}-\d{2}";
- string tel2 = @"\d{3}-\d{3}";
- string tel3 = @"\d{2}-\d{2}-\d{2}";
- while (true)
- {
- Console.WriteLine("Введите строку");
- string strok = Console.ReadLine();
- if (Regex.IsMatch(strok, tel1, RegexOptions.IgnoreCase))
- {
- Console.WriteLine("Номер 1");
- }
- if (Regex.IsMatch(strok, tel2, RegexOptions.IgnoreCase))
- {
- Console.WriteLine("Номер 2");
- }
- if (Regex.IsMatch(strok, tel3, RegexOptions.IgnoreCase))
- {
- Console.WriteLine("Номер 3");
- }
- else
- {
- Console.WriteLine("Неверный номер");
- }
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Вывести все номера телефонов, содержащиеся в сообщении соответсвующие форматам xx-xx-xx, xxx-xxx и xxx-xx-xx»
textual
Листинг программы
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- internal sealed class Program {
- static void Main() {
- String nums = @"
- 123-45-6 first
- 123-456 second
- 12-34-56 third
- other
- 123-45-67 -!sss//123
- 12-34-56-78-90
- 09-87-65 fourth
- ";
- new Regex(@"\d+(-?\d+)*").Matches(nums).Cast<Match>()
- .Select(v => v.Value)
- .Where(v => v.Count(c => c >= '0' && c <= '9') == 6)
- .ToList()
- .ForEach(v => Console.WriteLine(v));
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д