在vc++中使用boost的示例
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2014-07-11 20:30:05
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
首先要在开发环境下配置好boost
然后
#include <boost/regex.hpp>
下面是boost中查找字符串的功能
boost::regex_search 功能是查找字符串有两种模式
第一种不要迭代器的:这种只能查找第一个匹配的字符串后面的就查不到啦
如下:
string str="我爱你要我是你的888我爱你要我999是你的";
boost::regex el("(要)(.*?)(的)");
boost::smatch matches;
if(boost::regex_search(str, matches,el)){
logoutput((matches[0].str().c_str()));
logoutput((matches[1].str().c_str()));
logoutput((matches[2].str().c_str()));
logoutput((matches[3].str().c_str()));
}
上面代码输出结果为
要我是你的 要 我是你 的
matches[0]是对正则的完整匹配 matches[1] matches[2] matches[3]
后面是的子表达式的内容
第二种使用迭代器:可以查 找到所有匹配的字符串
如下:
//使用迭代器查找所有
string str="我爱你要我是你的888我爱你要我999是你的";
string::const_iterator start = str.begin();
string::const_iterator end = str.end();
boost::regex el("(要)(.*?)(的)");
boost::smatch matches;
//循环查找所有字符串
while(boost::regex_search(start,end, matches,el)){
logoutput((matches[0].str().c_str()));
logoutput((matches[1].str().c_str()));
logoutput((matches[2].str().c_str()));
logoutput((matches[3].str().c_str()));
start=matches[0].second;//更新查找的开始位置
}