forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex17_23.cpp
60 lines (51 loc) · 1.32 KB
/
ex17_23.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
52
53
54
55
56
57
58
59
60
/***************************************************************************
* @file main.cpp
* @author Queequeg
* @date 26 Nov 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//
// Exercise 17.21
// Rewrite your phone number program from 8.3.2 (p. 323) to use the
// valid function defined in this section.
#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
using std::istream;
using std::ostream;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <sstream>
using std::istringstream;
using std::ostringstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <regex>
using std::regex;
using std::sregex_iterator;
using std::regex_error;
int main() {
try {
regex reg("(\\d{ 4 })?([-])?(\\d{ 5 })");
string str;
while (getline(cin, str)) {
for (sregex_iterator b(str.cbegin(), str.cend(), reg), e; b != e; ++b) {
if (!(*b)[1].matched) // if if there is only the last 5 digits of index
cout << b->str(3); // show them
else
cout << b->str(); // show index entirely
cout << '\t';
}
}
}
catch (regex_error re) {
cout << re.what() << endl << re.code() << endl;
}
return 0;
}