Skip to content

Commit

Permalink
Create ex_23.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
qwert2603 committed Apr 12, 2015
1 parent 65a5109 commit 3192930
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions ch17/ex_23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

0 comments on commit 3192930

Please sign in to comment.