-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exercise12
148 lines (125 loc) · 3.54 KB
/
Exercise12
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
//
/*
Chapter 23 - Exercise 12
Write a program, based on the program that finds lines containing dates
(exercise 6), that finds all dates and reformats them to the ISO yyyy-mm-dd
format. The program should take an input file and produce and output file that
is identical to the input file except for the changed date formatting.
I decided not to do ALL date formats due to time.
*/
// https://github.com/l-paz91/principles-practice/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include <regex>
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
enum class RegexPattern
{
e1,
e2
};
// -----------------------------------------------------------------------------
vector<string> getDateNumbers(const string& pDate)
{
string num;
vector<string> dateNums;
for (char c : pDate)
{
if (isdigit(c))
{
num += c;
}
else
{
if (isspace(c))
{
continue;
}
dateNums.push_back(num);
num = "";
}
}
dateNums.push_back(num);
return dateNums;
}
// -----------------------------------------------------------------------------
void printAndReplace(vector<string>& pV, RegexPattern pPattern)
{
// yyyy-mm-dd
switch (pPattern)
{
case RegexPattern::e1:
{
// first pattern mm/dd/yyyy
// 2 matches
regex pat1{ R"((\D0?[1-9]|1[0-2])[-.\/](([0-2][0-9])|3[0-1])[-.\/]\d{4})" };
for (uint32 i = 0; i < pV.size(); ++i)
{
smatch matches;
if (regex_search(pV[i], matches, pat1))
{
cout << "\nReplacing: " << pV[i] << "\n<< with >> ";
string date = matches[0];
vector<string> dateNums = getDateNumbers(date);
string newDate = dateNums[2] + '-' + dateNums[0] + '-' + dateNums[1];
pV[i] = regex_replace(pV[i], regex(date), newDate);
cout << pV[i] << "\n\n";
}
}
break;
}
case RegexPattern::e2:
{
// second pattern: dd/mm/yyyy
// 7 matches
regex pat2{ R"((([0-2][0-9])|3[0-1])[-.\/](0?[1-9]|1[0-2])[-.\/]\d{4})" };
for (uint32 i = 0; i < pV.size(); ++i)
{
smatch matches;
if (regex_search(pV[i], matches, pat2))
{
cout << "\nReplacing: " << pV[i] << "\n<< with >> ";
string date = matches[0];
vector<string> dateNums = getDateNumbers(date);
string newDate = dateNums[2] + '-' + dateNums[1] + '-' + dateNums[0];
pV[i] = regex_replace(pV[i], pat2, newDate);
cout << pV[i] << "\n\n";
}
}
break;
}
default:
break;
}
}
// -----------------------------------------------------------------------------
void fct()
{
// get the information
vector<string> txtFile;
// https://github.com/l-paz91/principles-practice/blob/master/Chapter%2023/Exercise%206/dateExamples.txt
ifstream in{ "dateExamples.txt" };
for (string s; getline(in, s);)
{
txtFile.push_back(s);
}
cout << "Pattern mm/dd/yyyy" << endl;
printAndReplace(txtFile, RegexPattern::e1);
cout << "\nPattern dd/mm/yyyy" << endl;
printAndReplace(txtFile, RegexPattern::e2);
}
// -----------------------------------------------------------------------------
int main()
{
// done to keep an eye on memory tracking (it will always report as a leak if done in main)
fct();
cout << "\n";
pressKeyToContinue();
_CrtDumpMemoryLeaks();
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------