在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. java io系列11之 FilterOutputStream

    FilterOutputStream 介绍 FilterOutputStream 的作用是用来“封装其它的输出流,并为它们提供额外的功能”.它主要包括BufferedOutputStream, Dat ...

  2. Linux记录-JMX监控JAVA进程

    3.修改xxx.sh 加入export JAVA_OPTS="-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.mana ...

  3. SpringBoot+Thyemleaf

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...

  4. 牛客网数据库SQL实战(此处只有答案,没有表内容)

    1.查找最晚入职员工的所有信息   select * from employees order by hire_date desc limit 1; --limit n表示输出前n条数据,limit ...

  5. ajax、fetch、axios — 请求数据

    jquery ajax jq 的ajax是对原生XHR的封装,除此以外还增添了对JSONP的支持.用起来非常方便 用法: $.ajax({ url:发送请求的地址, data:数据的拼接,//发送到服 ...

  6. python代码块和小数据池

    id和is 在介绍代码块之前,先介绍两个方法:id和is,来看一段代码 # name = "Rose" # name1 = "Rose" # print(id( ...

  7. 5年GTD自我管理经验,一块听听

    我在胜利油田做了20多年的油田信息化工作,以前的我经常处于这样一种状态: 当我正在做着手边的一项事情时,头脑里却不断地蹦出来其它的事务,让我烦心不已,焦虑不安:PPT经常要拖到汇报当天的凌晨才做完,有 ...

  8. 谈谈java程序代码保护及license设计

    理论上讲,不存在牢不可破的漏洞,只是时间和成本问题.通常我们认为的不可破解,说的是破解需要难以接受的时间和成本.对于java程序来说,class文件很容易被反编译,所以理论上而言,对java程序做li ...

  9. Impala系列: Impala常用的功能函数

    --=======================查看内置的函数--=======================hive 不需要进入什么内置数据库, 即可使用 show functions 命令列出 ...

  10. Android手机特殊软件配置

    1. 安装360一键root http://root.360.cn/ 2.安装SSHdroid 用于启用手机的ssh 地址:https://www.apk20.com/apk/77332/v/7346 ...