使用Boost Regex 的regex_search进行遍历搜索
在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;
- }
结果如下:
- 192
- 168
- 1
- 1
在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;
- }
效果与上一例相同。
bool r=boost::regex_match( szStr , reg);
boost::regex reg( "\\d+" ); //查找字符串里的数字
if(boost::regex_search(szStr, mat, reg))
{
cout << "searched:" << mat[0] << endl;
}
}
使用Boost Regex 的regex_search进行遍历搜索的更多相关文章
- #include <boost/regex.hpp>
boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...
- boost::string or boost::regex
有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...
- c++ 使用boost regex库 总结
用java的时候觉得挺折腾,回头来弄c++才知道什么叫折腾...汗... 首先参考我写的这篇文章:http://www.cnblogs.com/qrlozte/p/4100892.html 我从sou ...
- boost regex expression
Boost.Regex provides three different functions to search for regular expressions 1. regex_match #inc ...
- profile对比std::regex与boost::regex的性能
c++11标准库的regex比boost库的regex之间的性能差距接近5倍,这是为什么?stackflow上也找到一篇post<c++11 regex slower than python&g ...
- C++中三种正则表达式比较(C regex,C ++regex,boost regex)
工作需要用到C++中的正则表达式,以下三种正则可供参考 1,C regex #include <regex.h> #include <iostream> #include &l ...
- vs 2005 使用 boost regex
第一步: Boost 入门及其VS2005下编译boost库 boost.regex库安装指南 深入浅出之正则表达式(一) C++中三种正则表达式比较(C regex,C ++regex,boo ...
- 解决Boost.Regex对中文支持不好的问题
解决Boost.Regex对中文支持不好的问题 - k.m.Cao - 博客频道 - CSDN.NET 解决Boost.Regex对中文支持不好的问题 k.m.Caov0.1 问题的提出: Boo ...
- boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET
boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...
随机推荐
- 二叉树建立及遍历 C++ 源码
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <stdlib.h> using namespace s ...
- ThinkPHP3.2中字段unique验证出错的解决方法
protected $_validate=array( array('stu_id','','学号已存在',1,'unique',1), ) 当一次插入多条数据时: 在进行循环 使用create验证时 ...
- Android中不显示标题
在网上找的用requestWindowFeature(Window.FEATURE_NO_TITLE)这一句报错. 后来找到另一种方法 1.在res/values/styles.xml中添加如下代码 ...
- OPCServer:使用Matrikon OPC Server Simulation
实验用模拟OPCServer 旧版(50M):Matrikon OPC Server Simulation(v1.5.0.0),百度网盘,密码: mcur 新版(157M):Matrikon OPC ...
- Spring boot中使用Mongodb
安装 使用Idea新建Spring boot工程时需要选择Mongodb 或者在工程中加入依赖 Maven: <dependency> <groupId>org.springf ...
- Windows10开机自动运行批处理、脚本等的方法
方法/步骤: 一:打开我的电脑, 在地址栏输入:“C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup”, 二,把自动运行批处理.脚 ...
- spring boot零碎知识点待补充
@Controller 和@RestController的区别 @RestController相当于同时使用了@Controller和@ResponseBody 即不会使用视图解析器,返回值直接返回 ...
- 【noip 2012】提高组Day1T3.开车旅行
Description 小A和小B决定利用假期外出旅行,他们将想去的城市从1到N编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市i 的海拔高度为Hi,城市i 和城市 ...
- 使用vlfeat 包中遇到的问题
run('..../setup'); vl_complie(); 编译成功,但是仍然出现Invalid MEX-file ‘E:\vlfeat-0.9.20\toolbox\mex\mexw64\vl ...
- list vector map set (转)
List封装了链表,Vector封装了数组, list和vector得最主要的区别在于vector使用连续内存存储的,他支持[]运算符,而list是以链表形式实现的,不支持[]. Vector对于随机 ...