1: //learn_regex3.cpp 3: #include <iostream> 4: #include <string> 5: #include <regex> //for regex, smatch 6: using namespace std; 8: int main() 9: { 10: cout << boolalpha; 12: string data("abc12def345ghi6789jk"); 13: string pattern("(\\d+)[a-z]+(\\d+)[a-z]+(\\d+)"); 14: regex r(pattern); 15: smatch m; 17: cout << string(30,'=') << endl; 18: cout << data << endl; 19: cout << pattern << endl; 21: cout << string(30,'=') << endl; 22: cout << regex_search(data, m, r) << endl; 23: cout << m.empty() << endl; 24: cout << m.str() << endl; 25: cout << m.prefix() << endl; 26: cout << m.suffix() << endl; 27: cout << m.size() << endl; 28: cout << m.position() << endl; 30: cout << string(30,'=') << endl; 31: cout << m[0].matched << " " << m[0] << endl 32: << m[1].matched << " " << m.str(1) << " " 33: << m.position(1) << " " << m.length(1) << endl 34: << m[2].matched << " " << m.str(2) << " " 35: << m.position(2) << " " << m.length(2) << endl 36: << m[3].matched << " " << m.str(3) << " " 37: << m.position(3) << " " << m.length(3) << endl; 38: //m[#].str() and m[#].length() also work, but not m[#].position() 39: } 40: /*Output: 41: ============================== 42: abc12def345ghi6789jk 43: (\d+)[a-z]+(\d+)[a-z]+(\d+) 44: ============================== 45: true 46: false 47: 12def345ghi6789 48: abc 49: jk 50: 4 51: 3 52: ============================== 53: true 12def345ghi6789 54: true 12 3 2 55: true 345 8 3 56: true 6789 14 4 57: */