-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathExer17_26.cpp
More file actions
41 lines (41 loc) · 1.12 KB
/
Exer17_26.cpp
File metadata and controls
41 lines (41 loc) · 1.12 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
36
37
38
39
40
41
// Note: this program uses ./data/person_info.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <regex>
using std::cout;
using std::endl;
using std::ifstream;
using std::ostringstream;
using std::string;
using std::regex;
using std::smatch;
using std::sregex_iterator;
using std::regex_replace;
using std::regex_constants::format_first_only;
int main(int argc, char* argv[])
{
if (argc != 2)
return -1;
ifstream is(argv[1]);
string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
regex r(phone); // a regex to find our pattern
smatch m;
string s;
string fmt = "$2.$5.$7 "; // reformat numbers to ddd.ddd.dddd
// read each record from the input file, write the first match only
while (getline(is, s)) {
unsigned i = 0;
ostringstream os;
for(sregex_iterator it(s.begin(), s.end(), r), end_it;
it != end_it; ++it, ++i) {
if(i == 0)
os << it->prefix() << it->str() << " ";
else
os << it->format(fmt);
}
cout << os.str() << endl;
}
return 0;
}