如何使用C ++ Boost的regex_iterator() [英] How to use C++ Boost's regex_iterator()

查看:119
本文介绍了如何使用C ++ Boost的regex_iterator()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压匹配字符串中的子串。木卫一遍历结果,我需要使用<一个href=\"http://www.boost.org/doc/libs/1_42_0/libs/regex/doc/html/boost_regex/ref/regex_iterator.html\"><$c$c>regex_iterator().

这是唯一的用法示例我已经找到,但我不明白的回调。可能有人给我函数的例子uage?


让我们假设我输入文本是:

 大家好,这是一段话
喇嘛喇嘛14 ..是的
日期1986年4月15日

我想获得:

 你好,大家,这个是一sentense,喇嘛是约会


解决方案

如果你不明白的例子中只有一部分是回调,考虑到:

 的std :: for_each的(M1,M2和放大器; regex_callback);

大致相当于:

 的(; M1 = 2;!++ M1){
    class_index [(* M1)[5]名为.str()+(* M1)[6]名为.str()] =(* M1).POSITION(5);
}

假设,你的情况,你要所有匹配存储在一个向量,你会写是这样的:

  //警告,未经测试:
提高:: sregex_iterator M1(text.begin(),text.end(),前pression);
提高:: sregex_iterator平方米;
的std ::矢量&lt;标准::字符串&GT;令牌;
对于(; M1 = 2;!++ M1){
    tokens.push_back(M1-GT&; STR())。
}

I am using Boost to match substrings in a string. Io iterate over the results, I need to use regex_iterator().

That is the only usage example I have found, but I do not understand the callback. Could someone give me an example uage of the function?


Let us assume that my input text is:

"Hello everybody this is a sentense
Bla bla 14 .. yes 
date 04/15/1986 
"

I want to get:

"Hello" "everybody" "this" "is" "a" "sentense" "bla" "yes" "date"

解决方案

If the only part of the example you don't understand is the callback, consider that:

std::for_each(m1, m2, &regex_callback);

is roughly equivalent to:

for (; m1 != m2; ++m1){
    class_index[(*m1)[5].str() + (*m1)[6].str()] = (*m1).position(5);
}

Assuming that, in your case, you want to store all the matches in a vector, you would write something like:

//Warning, untested:
boost::sregex_iterator m1(text.begin(), text.end(), expression);
boost::sregex_iterator m2;
std::vector<std::string> tokens;
for (; m1 != m2; ++m1){
    tokens.push_back(m1->str()).
}

这篇关于如何使用C ++ Boost的regex_iterator()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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