使用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- ...
随机推荐
- TRAFFIC ANALYSIS EXERCISE - Ransomer
catalogue . SCENARIO . QUESTIONS . Analysis:10.3.14.134 . Analysis:10.3.14.131 1. SCENARIO The pcap ...
- java8的Streams
首先看一个问题:在这个task集合中一共有多少个OPEN状态的?计算出它们的points属性和.在Java 8之前,要解决这个问题,则需要使用foreach循环遍历task集合:但是在Java 8中可 ...
- python 面向对象(二)成员
##################################总结########################### 类的成员: 变量: 实例变量 对象.属性=xxx 类变量 ...
- vue实现筛选功能,文字选中变色
<template> <Poptip trigger="hover" title="Title" content="content& ...
- Java 微信公众号导出所有粉丝(openId)
由于公众号换了公司主体,需要做迁移,玩家的openId数据需要做处理. (我是按我要的json格式,将粉丝导成了1万条数据的一个json文件) 文件格式: { "info":[ { ...
- Golang入门教程(十三)延迟函数defer详解
前言 大家都知道go语言的defer功能很强大,对于资源管理非常方便,但是如果没用好,也会有陷阱哦.Go 语言中延迟函数 defer 充当着 try...catch 的重任,使用起来也非常简便,然而在 ...
- awk指定[]为分隔符
root@standby[16:38:35]$ root@standby[16:38:35]$ cat tmp.log qq [8.8.8.8] 6.6.6.6 [1502301436.016] &q ...
- mvn项目压缩打包
通常情况下,maven打包结果为jar或war包.如果需要一并打包配置文件等参数,通过resources配置指定需要打包的文件参数,如下示例: <project> ... <!-- ...
- Spring MVC整合Mybatis 入门
本文记录使用Intellij创建Maven Web工程搭建Spring MVC + Mybatis 的一个非常简单的示例.关于Mybatis的入门使用可参考这篇文章,本文在该文的基础上,引入了Spri ...
- linux centos 7 nodejs 的安装
先SSH 进到终端 随便一目录,或者/temp下 下载nodejs: (如果下载不了,官网 https://nodejs.org/en/download/复制LINUX版本的链接过来) wget ht ...