在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. 【.net】“Newtonsoft.Json”已拥有为“Microsoft.CSharp”定义的依赖项。

    #事故现场: “Newtonsoft.Json”已拥有为“Microsoft.CSharp”定义的依赖项. #事故原因: 安装的Newtonsoft.Json版本为11.0.2,版本过高,与Micro ...

  2. java io系列25之 PrintWriter (字符打印输出流)

    更多内容请参考:java io系列01之 "目录" PrintWriter 介绍 PrintWriter 是字符类型的打印输出流,它继承于Writer.PrintStream 用于 ...

  3. 微信小程序遇到的知识点

    话不多说,都是干货!!! 页面的生命周期: 当打开一个页面的时候,会触发onLoad和onReady和onShow事件. onLoad和onReady只触发一次.除非卸载了页面就是触发了onUnloa ...

  4. SqlServer中的事务使用

    一.事务的概念和特点 事务(transaction)是恢复和并发控制的基本单位. 事务的特点 原子性:事务是一个工作单元,要都成功,要么的失败 例子:A付款给B,A余额-100,B余额+100,只能都 ...

  5. MQTT学习笔记

    因为工作需要,了解了一下MQTT.顺便记下来,现在还不会用. 一.概述 MQTT(Message Queuing Telemetyr Transport  消息队列遥测传输协议):基于发布/订阅(Pu ...

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

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

  7. 七、文件IO——I/O处理方式和文件锁

    7.1 I/O 处理方式 7.1.1 I/O处理的五种模型 阻塞I/O模型 若所调用的 I/O 函数没有完成相关的功能就会使进程挂起,直到相关数据到达才会返回.如 终端.网络设备的访问. 非阻塞模型 ...

  8. DateTime时间格式转换为Unix时间戳格式

    /// <summary> /// 将DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="date ...

  9. DAO层设计

    一.类图分析 二.参考文档 ( JavaBean中DAO设计模式介绍)(附:设计源码) 三.类图设计文件 百度云盘:https://pan.baidu.com/s/1i5xaS8P[Power Des ...

  10. 算法 - 链的操作(三) - 返回倒数第k个节点(no.23)

    输入一个链 : 1 -> 3 -> 5 -> 6 -> 8 输入 k  = 2 返回 6 这个节点 python(2.7) def getNode(head, k): if(h ...