Source of learn_regex2.cpp


  1: //learn_regex2.cpp

  3: #include <iostream>
  4: using std::cout;
  5: using std::endl;
  6: using std::boolalpha;

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

 11: #include <regex>
 12: using std::regex;
 13: using std::smatch; //This is new

 15: int main()
 16: {
 17:     cout << boolalpha;

 19:     string pattern("abc");
 20:     regex r(pattern);

 22:     smatch m;

 24:     cout << string(20, '=');
 25:     string data1("abc");
 26:     cout << regex_match(data1, m, r) << endl;
 27:     cout 
 28:         << m[0] << " "
 29:         << m.str() << " "
 30:         << m.str(0) << " "
 31:         << m[0].str() << " "
 32:         << *(m.begin()) << " "
 33:         << *m.begin() << " " 
 34:         << endl;

 36:     cout << string(20, '=');
 37:     cout << regex_search(data1, m, r) << endl;
 38:     cout 
 39:         << m[0] << " "
 40:         << m.str() << " "
 41:         << m.str(0) << " "
 42:         << m[0].str() << " "
 43:         << *(m.begin()) << " "
 44:         << *m.begin() << " " 
 45:         << endl;

 47:     cout << string(20, '=');
 48:     string data2("xabcy");
 49:     cout << regex_search(data2, m, r) << endl;
 50:     cout 
 51:         << m[0] << " "
 52:         << m.str() << " "
 53:         << m.str(0) << " "
 54:         << m[0].str() << " "
 55:         << *(m.begin()) << " "
 56:         << *m.begin() << " " 
 57:         << endl;
 58: }
 59: /*Output:
 60: ====================true
 61: abc abc abc abc abc abc
 62: ====================true
 63: abc abc abc abc abc abc
 64: ====================true
 65: abc abc abc abc abc abc
 66: */