当前位置:  开发笔记 > 编程语言 > 正文

你能用Boost.Regex解析一个流吗?

如何解决《你能用Boost.Regex解析一个流吗?》经验,为你挑选了1个好方法。

我正在玩Boost.Regex来解析单词和数字的字符串.这是我到目前为止:

#include 
#include 
#include 
#include 
#include 

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& what, make_iterator_range(m1, m2)) {
        cout << ":" << what[1].str() << ":" << what.position(1) << ":" << what.length(1) << endl;
    }

    return 0;
}

有没有办法告诉正则表达式从流而不是字符串解析?看起来应该可以使用任何迭代器.



1> Éric Malenfa..:

Boost.IOStreams有一个regex_filter,允许一个人在流上执行相当于regex_replace的操作.然而,看看实现,它似乎"作弊",因为它只是将整个流加载到缓冲区,然后在该缓冲区上调用Boost.Regex.

通过Boost.Regex 的" 部分匹配 "支持,可以在流的内容上进行正则表达式搜索,而无需将其完全加载到内存中.请查看页面末尾的示例.

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有