-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.17.cpp
51 lines (45 loc) · 1.03 KB
/
17.17.cpp
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
#include <regex>
#include <iostream>
#include <string>
#include <fstream>
using std::regex;
using std::regex_search;
using std::sregex_iterator;
using std::smatch;
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::ifstream;
using std::getline;
string readFile(std::istream &is) {
string content;
bool first = true;
for (string line; getline(is, line); ) {
if (!first) {
content += ' ';
}
content += line;
first = false;
}
return content;
}
int main() {
// Regex Pattern
string pattern = "[^c]ei";
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
regex r(pattern, regex::icase);
// Read file into string
ifstream infile("text.txt");
string file = readFile(infile);
// Report errors
for (sregex_iterator it(file.cbegin(), file.cend(), r), end;
it != end; ++it) {
auto pos = it->prefix().length();
pos = pos > 40 ? pos - 40 : 0;
cout << it->prefix().str().substr(pos)
<< "\n\t\t>>> " << it->str() << " <<<\n"
<< it->suffix().str().substr(0, 40) << endl;
}
return 0;
}