c++从regex_iterator获取匹配项
C++ get matches from regex_iterator
我正试图通过逐行读取文件来获得一些匹配。我的代码是:
std::regex e("id="(.+?)"|title="(.+?)"|summary="(.+?)"|first="(.+?)"|last="(.+?)"");
std::regex_iterator<std::string::iterator> rit ( line.begin(), line.end(), e );
std::regex_iterator<std::string::iterator> rend;
while (rit!=rend) {
std::cout << rit->str() << std::endl;
++rit;
}
我已经尝试使用regex_search和smatch对象,但它停止在行的第一个匹配。我发现regex_iterator完成了这项工作,但它给了我整个匹配(例如id="123456",而不是123456),这是有意义的,但我只需要数字。
根据http://www.cplusplus.com/reference/regex/regex_iterator/operator*/的解引用,迭代器会给我一个match_results对象,但我不知道如何声明一个(它总是给我一个糟糕的参数列表)。如何让迭代器给我一个smatch对象?
在match_results
对象上调用str()
将返回整个当前匹配。要查看各个子匹配项,在调用中传递一个索引:
for (int i = 0; i < rit->size(); ++i)
std::cout << rit->str(i) << 'n';
您可能必须遍历匹配,每次调用regex_match
以每次获得更新的match_results
对象。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- 从 iterator.first 获取价值
- 给定一个 std::set::iterator,获取一个指向下一个元素的迭代器
- 如何使用regex_token_iterator<std::string::iterator>通过迭代器本身获取子匹配的原始字符串的位置?
- 我能否获取模板类型的"iterator",无论该类型是数组还是类似 STL 的容器?
- 获取 std::vector<std::string>::iterator 的索引