Source of ValidateTelNums_shell.cpp


  1: //ValidateTelNums_shell.cpp

  3: #include <iostream>
  4: #include <iomanip>
  5: #include <string>
  6: #include <regex>
  7: using namespace std;

  9: int main(int argc, char* argv[])
 10: {
 11:     string phonePattern = 
 12:         "^(\\(?)"   //1 Optional left parenthesis
 13:         "(\\d{3})"  //2 Exactly 3 digits
 14:         "(\\)?)"    //3 Optional right parenthesis
 15:         "([-. ]?)"  //4 An optional dash, period or blank space
 16:         "(\\d{3})"  //5 Exactly 3 digits
 17:         "([-. ]?)"  //6 An optional dash, period or blank space
 18:         "(\\d{4})$";//7 Exactly 4 digits
 19:     regex rPhone(phonePattern);
 20:     string phoneNumber;
 21:     cout << boolalpha;
 22:     int lineCount = 0;
 23:     while (getline(cin, phoneNumber))
 24:     {
 25:         smatch m;
 26:         bool matched =  regex_match(phoneNumber, m, rPhone);
 27:         cout << "matched = " << matched << "\t";
 28:         bool valid;
 29:         if (matched)
 30:         {
 31:             valid = //Fill in test here ...
 32:         }
 33:         else
 34:             valid = false;
 35:         cout << setw(2) << ++lineCount << " valid = " << valid << endl;
 36:     }
 37: }