Skip to content

Commit

Permalink
ex17.14.15.16
Browse files Browse the repository at this point in the history
Queequeg92 committed Nov 19, 2014
1 parent 55ddacb commit 0764f1d
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions ch17/ex17.14.15.16/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/***************************************************************************
* @file main.cpp
* @author Queequeg
* @date 19 Nov 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//!
//! 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:]]*e[[:alpha:]]*i[[:alpha:]]*c[[: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;
}
return 0;
}

0 comments on commit 0764f1d

Please sign in to comment.