<regex>
regex_match():與整個字符串進行匹配,匹配成功返回true.
regex_search(): 匹配的是子字符串,此外可能有子匹配(對應匹配中的子模式)
regex_replace(): 匹配並替換
regex_iterator: 迭代器,遍歷一個字符序列,查找出匹配給定模式的所有子字符序列。
在任何重復符號(?,*,+及{})之后放一個?,會使匹配器查找最短匹配而非最長匹配。
void match() { ifstream in("file.txt"); if (!in) cerr << "no file"<<endl; regex pat{R"(^A*b+c?$)"}; int lineno = 0; for (string line; getline(in, line);) { ++lineno; smatch matches; //匹配的字符串保存在這里 if (regex_search(line, matches, pat)) { cout << lineno << ":" << matches[0] <<endl; } } }
void test() { string str = "am dd dbd& ++e*dd ddd"; regex pat {R"(\s+(\w+))"}; for (sregex_iterator p(str.begin(), str.end(), pat); p != sregex_iterator{}; ++p) cout << (*p)[1] << endl; } //sregex_iterator的含義就是regex_iterator<string>