您可以使用Boost.Regex解析流? [英] Can you use Boost.Regex to parse a stream?

查看:110
本文介绍了您可以使用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\t 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解析流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆