在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. OS + linux proxy

    s Linux主机通过代理服务器进行网络连接 http://www.linuxidc.com/Linux/2015-01/111703.htm 新手用Linux做代理服务器 三招搞定 http://b ...

  2. Docker CE 各安装方法

    1.Docker CE 镜像源站 使用官方安装脚本自动安装 curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 2.U ...

  3. nginx request_time 和upstream_response_time

    1.request_time 官网描述:request processing time in seconds with a milliseconds resolution; time elapsed ...

  4. js静态方法与实例方法定义,js回调方法定义

    主要为了回调方法,随便把静态言法和实例方法也回顾一下. <script type="text/javascript"> var fun = { //下面是静态方法(第一 ...

  5. http的三次握手四次挥手

    http : 是一种超文本传输协议 传输指的是在客户端和服务器之间进行传输数据 http不仅仅可以传输文本,还可以传输图片.音频.视频等内容 https : 是一种更加安全的传输协议 在普通的传输协议 ...

  6. 052、overlay如何实现跨主机通信?(2019-03-19 周二)

    参考https://www.cnblogs.com/CloudMan6/p/7305989.html   今天开始学习 overlay 网络跨主机通信的原理   root@host01:~# ufw ...

  7. 解决浏览器跨域限制方案之WebSocket

    WebSocket是在HTML5中引入的浏览器与服务端的通信协议,可以类比HTTP. 可以在支持HTML5的浏览器版本中使用WebSocket进行数据通信,常见的案例是使用WebSocket进行实时数 ...

  8. 细说shiro之四:在web应用中使用shiro

    官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...

  9. JS 比较两个数组是否相等 是否拥有相同元素

    Javascript怎么比较两个数组是否相同?JS怎么比较两个数组是否有完全相同的元素?Javascript不能直接用==或者===来判断两个数组是否相等,无论是相等还是全等都不行,以下两行JS代码都 ...

  10. telnet客户端程序

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types. ...