Advertisement
sylviapsh

ExtractDatesFromTextPrintThemInCanadianFormat

Jan 30th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5.  
  6. class ExtractDatesFromTextPrintThemInCanadnFormat
  7. {
  8.   //Write a program that extracts from a given text all dates that match the format DD.MM.YYYY. Display them in the standard date format for Canada.
  9.  
  10.   static DateTime ParseDate(string dateAsString)
  11.   {
  12.     DateTime date = DateTime.ParseExact(dateAsString, "dd.MM.yyyy", CultureInfo.InvariantCulture);
  13.     return date;
  14.   }
  15.  
  16.   static void Main()
  17.   {
  18.     string text = "ПРЕДСТОЯЩИ ДАТИ CAE - 2 /09.02.2013/ CAE - 1 16.02.2013 TKT Module 1 - 2 23.02.2013. CAE   На компютър   София  16.02.2013  от: 09/02 до: 16/02     01.02.2013  318.00 лв. FCE    На компютър   София  23.02.2013  от: 16/02 до: 23/02     08.02.2013  305.00 лв.    Регистрирай се";
  19.     List <DateTime> datesList = new List<DateTime>();
  20.  
  21.     foreach (Match item in Regex.Matches(text, @"\b[0-9]{2}.[0-9]{2}.[0-9]{4}\b"))
  22.     {
  23.       DateTime date = ParseDate(item.Value);
  24.       datesList.Add(date);
  25.     }
  26.  
  27.     foreach (DateTime date in datesList)
  28.     {
  29.       Console.WriteLine(date.ToString(CultureInfo.GetCultureInfo("en-CA").DateTimeFormat.ShortDatePattern));
  30.     }
  31.  
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement