Source of learn_regex1.cpp


  1: //learn_regex1.cpp

  3: #include <iostream>
  4: #include <string>
  5: #include <regex> //for regex
  6: using namespace std;
  7: using namespace std::regex_constants;
  8: //for format_first_only, format_no_copy

 10: int main()
 11: {
 12:     cout << boolalpha;

 14:     string pattern("abc");
 15:     regex r(pattern);

 17:     cout << string(20,'=') << endl;
 18:     string data1("abc");
 19:     cout << regex_match(data1, r) << endl;
 20:     cout << regex_search(data1, r) << endl;

 22:     cout << string(20,'=') << endl;
 23:     string data2("xabcy");
 24:     cout << regex_match(data2, r) << endl;
 25:     cout << regex_search(data2, r) << endl;

 27:     cout << string(20,'=') << endl;
 28:     string format("def");
 29:     cout << regex_replace(data1, r, format) << endl;
 30:     cout << regex_replace(data2, r, format) << endl;
 31:     cout << regex_replace(data2, r, format, format_no_copy) << endl;

 33:     cout << string(20,'=') << endl;
 34:     string data3("xabcyabcz");
 35:     cout << regex_replace(data3, r, format) << endl;
 36:     cout << regex_replace(data3, r, format, format_first_only) << endl;
 37: }
 38: /*Output:
 39: ====================
 40: true
 41: true
 42: ====================
 43: false
 44: true
 45: ====================
 46: def
 47: xdefy
 48: def
 49: ====================
 50: xdefydefz
 51: xdefyabcz
 52: */