-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExtractAllDatesFromFile.cs
69 lines (64 loc) · 1.99 KB
/
ExtractAllDatesFromFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* 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.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
class ExtractAllDatesFromFile
{
static void Main(string[] args)
{
try
{
Console.Write("Path:");
string path = @Console.ReadLine();
List<string> file = new List<string>();
List<DateTime> dates = new List<DateTime>();
StreamReader reader = new StreamReader(@path);
using (reader)
{
string line = reader.ReadLine();
while (line != null)
{
file.Add(line);
line = reader.ReadLine();
}
}
for (int i = 0; i < file.Count; i++)
{
DateTime date;
foreach (Match item in Regex.Matches(file[i], @"\b\d{2}.\d{2}.\d{4}\b"))
{
if (DateTime.TryParseExact(item.Value, "dd.MM.yyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
Console.WriteLine(date.ToString(CultureInfo.GetCultureInfo("en-CA").DateTimeFormat.ShortDatePattern));
}
}
}
}
catch (ArgumentException ae)
{
Console.Error.WriteLine(ae.Message);
}
catch (FormatException fe)
{
Console.Error.WriteLine(fe.Message);
}
catch (FileLoadException fle)
{
Console.Error.WriteLine(fle.Message);
}
catch (FileNotFoundException fnfe)
{
Console.Error.WriteLine(fnfe.Message);
}
catch (AccessViolationException ave)
{
Console.Error.WriteLine(ave.Message);
}
}
}