forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//exercise 17.25 | ||
//Rewrite your phone program so that it writes only the first phone number for each person | ||
|
||
#include <iostream> | ||
#include <regex> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
string pattern = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})"; | ||
string fmt = "$2.$5.$7"; | ||
regex r(pattern); | ||
string s; | ||
|
||
int main() | ||
{ | ||
while(getline(cin,s)) | ||
{ | ||
smatch result; | ||
regex_search(s,result,r); | ||
if(!result.empty()) | ||
{ | ||
cout<<result.prefix()<<result.format(fmt)<<endl; | ||
} | ||
else | ||
{ | ||
cout<<"Sorry, No match."<<endl; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//exercise 17.27 | ||
//Write a program that reformats a nine-digit zip code as ddddd-dddd. | ||
|
||
#include <iostream> | ||
#include <regex> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
string pattern = "(\\d{5})([.- ])?(\\d{4})"; | ||
string fmt = "$1-$3"; | ||
|
||
regex r(pattern); | ||
string s; | ||
|
||
|
||
int main() | ||
{ | ||
while(getline(cin,s)) | ||
{ | ||
smatch result; | ||
regex_search(s,result, r); | ||
|
||
if(!result.empty()) | ||
{ | ||
cout<<result.format(fmt)<<endl; | ||
} | ||
else | ||
{ | ||
cout<<"Sorry, No match."<<endl; | ||
} | ||
|
||
} | ||
return 0; | ||
} |