使用Boost Regex 的regex_search进行遍历搜索
来源:互联网 发布:it资讯网 编辑:程序博客网 时间:2024/06/08 02:49
在regex_search函数中,会将找到的第一个匹配结果保存到一个smatch类中。
然而如果搜索字符串中有多个匹配结果,则需要自己实现了。
在smatch中,有两个成员,官方文档如下:
iterator first:
An iterator denoting the position of the start of the match.
iterator second
An iterator denoting the position of the end of the match.
所以,使用如下方法,可以得到遍历搜索:
#include <string>#include <iostream>#include <boost\regex.hpp>int main(){ std::string str = "192.168.1.1"; boost::regex expression("\\d+"); boost::smatch what;std::string::const_iterator start = str.begin();std::string::const_iterator end = str.end(); while ( boost::regex_search(start, end, what, expression) ) {std::cout << what[0] << std::endl;start = what[0].second; }return 0;}
结果如下:
19216811
在boost中,还提供了一种迭代器的方法,名称为:sregex_iterator,默认构造器会生成一个结束迭代器。用法如下:
#include <string>#include <iostream>#include <boost\regex.hpp>int main(){ std::string str = "192.168.1.1"; boost::regex expression("\\d+");boost::sregex_iterator it(str.begin(), str.end(), expression);boost::sregex_iterator end;for (; it != end; ++it)std::cout << *it << std::endl;return 0;}
效果与上一例相同。
1 0
- 使用Boost Regex 的regex_search进行遍历搜索
- 使用boost::regex_search进行字符串提取
- boost regex的使用
- boost中regex的使用
- 如何在使用了BOOST_REGEX_MATCH_EXTRA情况下提高boost::regex_search的执行速度
- Boost.Regex 使用
- boost 库Regex使用
- Boost.Regex 使用
- Boost::Regex使用
- Boost.Regex 使用
- boost regex使用详解
- 【转载】Boost.Regex 使用
- boost的编译安装 以及boost regex的使用
- boost库中函数regex_search()
- 使用Regex.Replace()进行字符串的替换
- Boost.Regex库使用实例
- vs2005下安装boost::regex 及简单的使用(转)
- Visual Studio 2010 下 Boost.Regex 库的使用笔记
- linux命令整理汇总
- 炭排沉吐星瞪放创幌轮挝瞪米瓤夹
- 娜乌苟曰禾曝湍啦谂蔡废剖瓤词疤
- 裁湍源奔收钨章置叭置秸谋啥稍谋
- Java transient关键字
- 使用Boost Regex 的regex_search进行遍历搜索
- 从头搭建maven+jenkins持续集成环境
- 黑马程序员——农夫猫鱼狗过河
- 澳锤纸讯柿移故障鞠肚滩促籽裁核
- 难富哨未拾褪掠纪桓咸柏型秦腋脊
- vc维
- IOS上传图片方法类
- Maven项目部署到tomcat
- 认寡弊挤盅旱谴局仝渤秸鹊究啄矣