-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexec23-6.cpp
More file actions
35 lines (31 loc) · 1.07 KB
/
exec23-6.cpp
File metadata and controls
35 lines (31 loc) · 1.07 KB
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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
// Find dates in a text file. Write out each line containing at least one date.
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Usage: " << argv[0] << " filename\n";
return 0;
}
ifstream ifs(argv[1]);
if (!ifs) {
cerr << "can't open input file " << argv[1] << endl;
return 1;
}
vector<regex> date_patterns = {
regex(R"(\d{4}-\d{1,2}-\d{1,2})"), // 2007-06-05
regex(R"(\d{4}/\d{1,2}/\d{1,2})"), // 2007/06/05
regex(R"(\d{1,2}/\d{1,2}/\d{4})"), // 6/5/2007
regex(R"([A-Za-z]+ \d{1,2}, \d{4})"), // June 5, 2007
regex(R"(\d{1,2} [A-Za-z]+ \d{4})") // 5 June 2005
};
string line;
for (int lineno = 1; getline(ifs, line); ++lineno)
if (any_of(date_patterns.begin(), date_patterns.end(), [&line](const regex& p) { return regex_search(line, p); }))
cout << lineno << ": " << line << '\n';
return 0;
}