forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex17_14_15_16.cpp
81 lines (68 loc) · 2.43 KB
/
ex17_14_15_16.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/***************************************************************************
* @file main.cpp
* @author Queequeg
* @date 19 Nov 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note There are some bugs in gcc(include the latest version 4.9.2)
* to handle regular expression.To compile this program, please
* turn to other compilers such as msvs2013 and clang.
***************************************************************************/
//
// Exercise 17.14
// Write several regular expressions designed to trigger various errors.
// Run your program to see what output your compiler generates for each error.
// Exercise 17.15
// Write a program using the pattern that finds word that violate the
// "i before e except after c" rule. Have your program prompt the user to
// supply a word and indicate whether the word is okay or not. Test your
// program with words that do and do not violate the rule.
// Exercise 17.16
// What would happen if your regex object in the previous program were
// initialized with "[^c]ei"? Test your program using that pattern to see
// whether your expectations were correct.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include <regex>
using std::regex;
using std::regex_error;
int main()
{
// for ex17.14
// error_brack
try{
regex r("[[:alnum:]+\\.(cpp|cxx|cc)$", regex::icase);
}
catch(regex_error e)
{
cout << e.what() << " code: " << e.code() << endl;
}
// for ex17.15
regex r("[[:alpha:]]*[^c]ei[[:alpha:]]*", regex::icase);
string s;
cout << "Please input a word! Input 'q' to quit!" << endl;
while(cin >> s && s != "q")
{
if(std::regex_match(s, r))
cout << "Input word " << s << " is okay!" << endl;
else
cout << "Input word " << s << " is not okay!" <<endl;
cout << "Please input a word! Input 'q' to quit!" << endl;
}
cout << endl;
// for ex17.16
r.assign("[^c]ei", regex::icase);
cout << "Please input a word! Input 'q' to quit!" << endl;
while(cin >> s && s != "q")
{
if(std::regex_match(s, r))
cout << "Input word " << s << " is okay!" << endl;
else
cout << "Input word " << s << " is not okay!" <<endl;
cout << "Please input a word! Input 'q' to quit!" << endl;
}
return 0;
}