Source of regex_matches.cpp


  1: //regex_matches.cpp (based on a Deitel example)
  2: //Demonstrates the use of regular expressions.

  4: #include <iostream>
  5: using namespace std;

  7: #include <string>
  8: using std::string;

 10: #include "boost/regex.hpp"
 11: using boost::regex;
 12: using boost::smatch;
 13: using boost::regex_search;
 14: using boost::match_not_dot_newline;

 16: int main()
 17: {
 18:     cout << endl;

 20:     //Create a regular expression
 21:     boost::regex expression("J.*\\d[0-35-9]-\\d\\d-\\d\\d\\.");

 23:     //Create a string to be tested
 24:     string string1 = "Jane's Birthday is 05-12-75.\n"
 25:         "Dave's Birthday is 11-04-68.\n"
 26:         "John's Birthday is 04-28-73.\n"
 27:         "Joe's Birthday is 12-17-77.";

 29:     //Create an smatch object to hold the search results
 30:     smatch match;

 32:     //Match regular expression to string and print out all matches
 33:     //match_not_dot_newline causes . not to match \n
 34:     while (regex_search(string1, match, expression, match_not_dot_newline))
 35:     {
 36:         cout << match << endl;    //Print matching string
 37:         string1 = match.suffix(); //Remove matched substring from string
 38:     }
 39:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 40: }