Advertisement
son4etyyy

ExtractDates

Feb 3rd, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8.  
  9. namespace _19.EtractDates
  10. {
  11.     /*Write a program that extracts from a given text all dates that match the format DD.MM.YYYY.
  12.      * Display them in the standard date format for Canada.*/
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             string text = "I was born at 14.06.1980. My sister was born at 3.7.1984. In 5/1999 I graduated my high school. The law says (see section 7.3.12) that we are allowed to do this (section 7.4.2.9).";
  18.  
  19.             foreach (var extracted in Regex.Matches(text, @"((((0?[1-9])|[12][0-9]|3[01])\.((0?[13578])|(1[02])))|(((0?[1-9])|[12][0-9]|30)\.((0?[469])|11))|(((0?[1-9])|[12][0-9])\.(0?2)))\.\d{4}"))
  20.             {
  21.                 string extractedToString = Convert.ToString(extracted);
  22.                 DateTime date = DateTime.ParseExact(extractedToString, "dd.MM.yyyy", CultureInfo.InvariantCulture);
  23.                 Console.WriteLine(date.ToString(CultureInfo.GetCultureInfo("en-CA")));
  24.             }
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement