使用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- ...
随机推荐
- C#设计模式(3)——抽象工厂模式
1.抽象工厂模式介绍 上一篇我们了解了工厂模式,知道工厂模式可以解决简单工厂的缺陷(简单工厂添加新产品时要修改工厂类,不符合开闭原则),但是简单工厂和工厂模式都是只生产一种产品(前边的简单工厂和工厂都 ...
- PHP7 网络编程(五)进程间通信【待】
https://blog.csdn.net/godleading/article/details/78391159
- HIbernate处理数据更新丢失
使用乐观锁的机制处理: 第一步: 在持久类中添加version属性,并且添加对应的get.set方法; 第二步: 在全局配置文件中配置节点<version name="version& ...
- markdown & mathjax 初学笔记 latex
stackedit 1.标题大小和# # 数量代表标题大小,越多越小 2.* 斜体 * 3.** 粗体 ** 4.*** 又粗又斜 *** PS:符号紧贴 5. 分隔符 - - - 三个减号 PS: ...
- 【bzoj 2049】Cave 洞穴勘测
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- js获取当前城市名
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- transition过渡动画
过渡动画必须写在<transition></transition>标签内,配合其他标签使用. 例子: <transition name="fade" ...
- 2.2 if语句
if判断语句 <1>if判断语句介绍 if语句是用来进行判断的,其使用格式如下: if 要判断的条件: 条件成立时,要做的事情 demo1:(demo的中文意思:演示.案例) age = ...
- GIT原理【摘】
- 第26月第9天 getActionBar为空的解决办法
1.python 包路径 export PYTHONPATH=路径 https://blog.csdn.net/machinezj/article/details/60137666 2.getActi ...