1. smatch允许获得匹配的上下文
匹配类型有两个名为 prefix 和 suffix的成员, 分别返回表示输入序列中当前匹配之前和之后部分的 SSub_matCh 对象。
一个ssub_match 对象有两个名为 str 和 length 的成员, 分别返回匹配的 string 和该string 的大小
// 显示出匹配单词的上下文 // 循环头与之前一样 for (sregex_iterator it(file.begin(), file.end(), r), end_it; it != end_it; ++it) { auto pos = it->prefix().length(); // 前缀的大小 pos = pos > 40 ? pos - 40 : 0; // 我们想要最多40个字符 cout << it->prefix().str().substr(pos) // 前缀的最后一部分 << " >>> " << it->str() << " <<suffix().str().substr(0, 40) // 后缀的第一部分 << endl; }
这些 *** 作也适用于cmatch、wsmatch、wcmatch和对应的csub_match、wssub_match和wcsub_match
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)