-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exercise14
99 lines (80 loc) · 2.55 KB
/
Exercise14
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
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
//
/*
Chapter 23 - Exercise 14
Write a program that, like the one in section 23.8.7, can be used to experiment
with pattern matching by typing in a pattern. However, have it read a file into
memory (representing a line break with the new line character, '\n'), so that
you can experiment with patterns spanning line breaks. Test it and document a
dozen test patterns.
*/
// https://github.com/l-paz91/principles-practice/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include <regex>
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
void fct()
{
// accept a pattern and a set of lines from input
// check the pattern and search for lines with that pattern
regex pattern;
string input;
cout << "Enter pattern: ";
getline(cin, input);
try
{
pattern = input;
cout << "\nPattern: " << input << "\n";
}
catch (regex_error e)
{
cout << input << " is not a valid regular expression\n";
pressKeyToContinue();
exit(1);
}
// https://github.com/l-paz91/principles-practice/blob/master/Chapter%2023/Exercise%208/CorporateGibberish.txt
string file{ "CorporateGibberish.txt" };
ifstream txtFile{ file };
int lineNum = 0;
string newString;
for (string line; getline(txtFile, line);)
{
newString += line + '\n';
++lineNum;
smatch matches;
if (regex_search(line, matches, pattern))
{
cout << "----Matched in original text----" << '\n';
cout << "Line " << lineNum << ": " << line << "\n";
for (uint32 i = 0; i < matches.size(); ++i)
{
cout << "\tMatches[" << i << "]: " << matches[i] << '\n';
}
}
}
// when the file has newlines represented by '\n'
smatch matches;
if (regex_search(newString, matches, pattern))
{
cout << "----Matched in condensed text----" << '\n';
for (uint32 i = 0; i < matches.size(); ++i)
{
cout << "\tMatches[" << i << "]: " << matches[i] << '\n';
}
}
}
// -----------------------------------------------------------------------------
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;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------