在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.

所以,使用如下方法,可以得到遍历搜索:

  1. #include <string>
  2. #include <iostream>
  3. #include <boost\regex.hpp>
  4. int main()
  5. {
  6. std::string str = "192.168.1.1";
  7. boost::regex expression("\\d+");
  8. boost::smatch what;
  9. std::string::const_iterator start = str.begin();
  10. std::string::const_iterator end = str.end();
  11. while ( boost::regex_search(start, end, what, expression) )
  12. {
  13. std::cout << what[0] << std::endl;
  14. start = what[0].second;
  15. }
  16. return 0;
  17. }

结果如下:

  1. 192
  2. 168
  3. 1
  4. 1

在boost中,还提供了一种迭代器的方法,名称为:sregex_iterator,默认构造器会生成一个结束迭代器。用法如下:

  1. #include <string>
  2. #include <iostream>
  3. #include <boost\regex.hpp>
  4. int main()
  5. {
  6. std::string str = "192.168.1.1";
  7. boost::regex expression("\\d+");
  8. boost::sregex_iterator it(str.begin(), str.end(), expression);
  9. boost::sregex_iterator end;
  10. for (; it != end; ++it)
  11. std::cout << *it << std::endl;
  12. return 0;
  13. }

效果与上一例相同。

 
如果不需要遍历,只需要匹配,那更简单:
    boost::regex reg( szReg );
    bool r=boost::regex_match( szStr , reg);
或是需要放入一个cmatch 中:
{
    boost::cmatch mat;
    boost::regex reg( "\\d+" );    //查找字符串里的数字
    if(boost::regex_search(szStr, mat, reg))
    {
        cout << "searched:" << mat[0] << endl;
    }
}

使用Boost Regex 的regex_search进行遍历搜索的更多相关文章

  1. #include <boost/regex.hpp>

    boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...

  2. boost::string or boost::regex

    有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...

  3. c++ 使用boost regex库 总结

    用java的时候觉得挺折腾,回头来弄c++才知道什么叫折腾...汗... 首先参考我写的这篇文章:http://www.cnblogs.com/qrlozte/p/4100892.html 我从sou ...

  4. boost regex expression

    Boost.Regex provides three different functions to search for regular expressions 1. regex_match #inc ...

  5. profile对比std::regex与boost::regex的性能

    c++11标准库的regex比boost库的regex之间的性能差距接近5倍,这是为什么?stackflow上也找到一篇post<c++11 regex slower than python&g ...

  6. C++中三种正则表达式比较(C regex,C ++regex,boost regex)

    工作需要用到C++中的正则表达式,以下三种正则可供参考 1,C regex #include <regex.h> #include <iostream> #include &l ...

  7. vs 2005 使用 boost regex

    第一步: Boost 入门及其VS2005下编译boost库  boost.regex库安装指南  深入浅出之正则表达式(一)  C++中三种正则表达式比较(C regex,C ++regex,boo ...

  8. 解决Boost.Regex对中文支持不好的问题

    解决Boost.Regex对中文支持不好的问题 - k.m.Cao - 博客频道 - CSDN.NET 解决Boost.Regex对中文支持不好的问题 k.m.Caov0.1   问题的提出: Boo ...

  9. boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET

    boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...

随机推荐

  1. VirtualBox安装Ubuntu14.04

    创建虚拟机 点击 新建(N) 设置虚拟机的名称,类型与版本,如下图所示: 分配虚拟机的内存大小,受PC实际内存影响,暂时设置为2G,如下图所示: 分配虚拟机的硬盘大小,默认即可,如下图所示: 分配虚拟 ...

  2. ContentType组件

    django提供的一个快速连表操作的组件 适用于:一个字段确定不了唯一: 如:pricepolicy表中,course_id和content_type中对应的课程类型id才能确定唯一: model.p ...

  3. Spark源码剖析 - SparkContext的初始化(三)_创建并初始化Spark UI

    3. 创建并初始化Spark UI 任何系统都需要提供监控功能,用浏览器能访问具有样式及布局并提供丰富监控数据的页面无疑是一种简单.高效的方式.SparkUI就是这样的服务. 在大型分布式系统中,采用 ...

  4. 开源框架.netCore DncZeus学习(一)npm安装

    今天看到一个不错的开源项目DncZeus, https://github.com/lampo1024/DncZeus 整个界面挺漂亮,而且权限做到了按钮级别,功能也较容易扩展,刚好学习VUE纯看文章很 ...

  5. Retrofit的通讯方式示例

    Retrofit有两种通讯方式,同步和异步 异步方式: APIService req; req = RetrofitManager.getInstance().createReq(APIService ...

  6. 数据库范式:1NF,2NF,3NF,BCNF浅析

    在设计与操作维护数据库时,最关键的问题就是要确保数据能够正确地分布到数据库的表中.使用正确的数据结构,不仅有助于对数据库进行相应的存取操作,还可以极大地简化应用程序中的其他内容(查询.窗体.报表.代码 ...

  7. ELK平台搭建(上)

    一.目的 为指导在Centos6.8系统下搭建标准ELK平台的工作. 二.定义 Elasticsearch Logstash Kibana结合Redis协同工作. 三.适用范围 适用于运营维护组运维工 ...

  8. 【mmall】学习Spring要善用Spring的Github

    官网:https://projects.spring.io/spring-framework 宠物医院项目(非常经典的Spring项目):https://github.com/spring-proje ...

  9. Linux常用命令(二)查找当前ip地址

    查询当地ip地址(没错就是这么短): /sbin/ifconfig

  10. 列式数据库~clickhouse日常管理

    clickhouse日常管理一 变量相关  1 查看变量     system.setting相关表  2 设置变量     set variables= 请注意这里是session级别,如果想永久生 ...