forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex17_23.cpp
60 lines (49 loc) · 1.11 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.23
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
#include <regex>
using std::regex;
using std::sregex_iterator;
using std::smatch;
bool valid(const smatch& m);
int main()
{
string zipcode =
"(\\d{5})([-])?(\\d{4})?\\b";
regex r(zipcode);
smatch m;
string s;
while (getline(cin, s))
{
//! for each matching zipcode number
for (sregex_iterator it(s.begin(), s.end(), r), end_it;
it != end_it; ++it)
{
//! check whether the number's formatting is valid
if (valid(*it))
cout << "valid zipcode number: " << it->str() << endl;
else
cout << "invalid zipcode number: " << s << endl;
}
}
return 0;
}
bool valid(const smatch& m)
{
if ((m[2].matched)&&(!m[3].matched))
return false;
else
return true;
}