Can you use Boost.Regex to parse a stream?(您可以使用 Boost.Regex 来解析流吗?)
问题描述
我在玩 Boost.Regex 来解析单词和数字的字符串.这是我目前所拥有的:
I was playing around with Boost.Regex to parse strings for words and numbers. This is what I have so far:
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/range.hpp>
using namespace std;
using namespace boost;
int main()
{
regex re
(
"("
"([a-z]+)|"
"(-?[0-9]+(\.[0-9]+)?)"
")"
);
string s = "here is a list of Words. and some 1239.32 numbers to 3323 parse.";
sregex_iterator m1(s.begin(), s.end(), re), m2;
BOOST_FOREACH (const match_results<string::const_iterator>& what, make_iterator_range(m1, m2)) {
cout << ":" << what[1].str() << ":" << what.position(1) << ":" << what.length(1) << endl;
}
return 0;
}
有没有办法告诉正则表达式从流而不是字符串中解析?似乎应该可以使用任何迭代器.
Is there a way to tell regex to parse from a stream rather than a string? It seems like it should be possible to use any iterator.
推荐答案
Boost.IOStreams 有一个 regex_filter 允许在流上执行相当于 regex_replace 的操作.然而,从实现来看,它似乎是作弊",因为它只是将整个流加载到缓冲区中,然后在该缓冲区上调用 Boost.Regex.
Boost.IOStreams has a regex_filter allowing one to perform the equivalent of a regex_replace on a stream. However, looking at the implementation, it seems to "cheat" in that it simply loads the whole stream into a buffer and then calls Boost.Regex on that buffer.
可以使用部分匹配" 支持 Boost.Regex.查看页面末尾的示例.
Making a regex search on a stream's contents without having to entirely load it in memory can be done with the "partial match" support of Boost.Regex. Look at the example at the end of the page.
这篇关于您可以使用 Boost.Regex 来解析流吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:您可以使用 Boost.Regex 来解析流吗?
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07