Source of learn_regex4.cpp


  1: //learn_regex4.cpp

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

  8: int main()
  9: {
 10:     cout << boolalpha;

 12:     string ruler("01234567890123456789");
 13:     string data("12abc379gfehd3456");
 14:     string pattern("([a-f]+)(\\d{2})");
 15:     regex r(pattern);

 17:     cout << string(20,'=') << endl;
 18:     cout << ruler << endl;
 19:     cout << data << endl;
 20:     cout << pattern << endl;
 21:     cout << string(20,'=') << endl;

 23:     sregex_iterator i(data.begin(), data.end(), r), end;
 24:     while (i != end)
 25:     {
 26:         cout << i->str(0) << endl;
 27:         cout << i->prefix() << endl;
 28:         cout << i->suffix() << endl;
 29:         cout << string(20,'.') << endl;
 30:         cout << i->str(0) << endl;
 31:         cout << i->position(0) << endl;
 32:         cout << i->length(0) << endl;
 33:         cout << i->str(1) << endl;
 34:         cout << i->position(1) << endl;
 35:         cout << i->length(1) << endl;
 36:         cout << i->str(2) << endl;
 37:         cout << i->position(2) << endl;
 38:         cout << i->length(2) << endl;
 39:         cout << i->str(3) << endl;
 40:         cout << i->position(3) << endl;
 41:         cout << i->length(3) << endl;
 42:         cout << i->str(4) << endl;
 43:         cout << i->position(4) << endl;
 44:         cout << i->length(4) << endl;
 45:         cout << string(20,'-') << endl;
 46:         ++i;
 47:     }
 48: }
 49: /*Output:
 50: ====================
 51: 01234567890123456789
 52: 12abc379gfehd3456
 53: ([a-f]+)(\d{2})
 54: ====================
 55: abc37
 56: 12
 57: 9gfehd3456
 58: ....................
 59: abc37
 60: 2
 61: 5
 62: abc
 63: 2
 64: 3
 65: 37
 66: 5
 67: 2

 69: 17
 70: 0

 72: 17
 73: 0
 74: --------------------
 75: d34
 76: 9gfeh
 77: 56
 78: ....................
 79: d34
 80: 12
 81: 3
 82: d
 83: 12
 84: 1
 85: 34
 86: 13
 87: 2

 89: 17
 90: 0

 92: 17
 93: 0
 94: --------------------
 95: */