(四)boost库之正则表达式regex
(四)boost库之正则表达式regex
正则表达式可以为我们带来极大的方便,有了它,再也不用为此烦恼
头文件:
#include <boost/regex.hpp>
1、完全匹配
std::string str("abcd");
boost::regex reg( "a\\w*d" );
if (regex_match(str, reg))
{
std::cout << str << " is match" << std::endl;
}
else
{
std::cout << str << " is not match" << std::endl;
}
2、完全匹配并获取子串
const char* mail = "tengxun@qq.com";
boost::cmatch res;
//建立3个子表达式
boost::regex reg("(\\w+)@(\\w+).(\\w+)");
if (boost::regex_match(mail,res, reg))
{
//既可以通过迭代器获取数据, 也可以通过数组方式获取数据
for (boost::cmatch::iterator pos = res.begin(); pos != res.end(); ++pos)
{
std::cout << *pos << std::endl;
}
//res[0]存放匹配到的完整字符串
std::cout << "name:" << res[1] << std::endl;
}
3、查找, 当你不需要匹配整个字符串的时候,可以选择查找
const char* mail = "tengxun@qq.com.cn";
boost::cmatch res;
//建立3个子表达式
boost::regex reg("(\\w+)@(\\w+).(\\w+)");
if (boost::regex_search(mail,res, reg))
{
std::cout <<"**************************************" << std::endl;
//既可以通过迭代器获取数据, 也可以通过数组方式获取数据
for (boost::cmatch::iterator pos = res.begin(); pos != res.end(); ++pos)
{
std::cout << *pos << std::endl;
}
//res[0]存放匹配到的完整字符串
std::cout << "match :" << res[0] << std::endl << "name:" << res[1] << std::endl;
}
4、替换
替换匹配到的子字符串, 可以通过$N 引用第N个匹配到的值、$& 引用全匹配
#include <boost/algorithm/string.hpp>
void TestReplace()
{
//将tengxun@qq.com.cn 替换成tengxun@139.com.cn
std::string mail("tengxun@qq.com.cn");
//建立3个子表达式
boost::regex reg("(\\w+)@(\\w+).(\\w+)");
std::cout << boost::regex_replace(mail, reg, "$1@139.$3") << std::endl;
std::cout << boost::regex_replace(mail, reg, "my$1@$2.$3") << std::endl;
//自定义替换函数,regex_replace将匹配到的字符串数组传递给回调函数,由回调函数返回新的字符串
std::cout << boost::regex_replace(mail, reg, [](const boost::smatch &m){
return boost::to_upper_copy(m[0].str());
});
}
5、迭代
当需要从字符串中提取多个表达式时,可以采用迭代进行提取
std::string str("tengxun@qq.com, aa@tt.com, bb@qq.com");
boost::regex reg("(\\w+)@(\\w+).(\\w+)");
boost::sregex_iterator pos(str.begin(), str.end(), reg);
boost::sregex_iterator end;
while(pos != end)
{
std::cout << "[" << (*pos)[0] << "]";
++pos;
}
6、分词
#include <iostream>
#include <boost/regex.hpp>
void TestToken()
{
using namespace std;
using namespace boost;
string str("tengxun@qq.com, aa@tt.com, bb@qq.com");
regex reg("\\w+");
sregex_token_iterator pos(str.begin(), str.end(), reg);
while(pos != sregex_token_iterator())
{
cout << "[" << *pos << "]" ;
++pos;
}
cout << endl;
//如果最后一个参数args为-1,则把匹配到的字符串视为分隔符
regex split_reg(",");
pos = sregex_token_iterator(str.begin(), str.end(), split_reg, -1);
while(pos != sregex_token_iterator())
{
cout << "[" << *pos << "]" ;
++pos;
}
cout << endl;
//如果最后一个参数args为正数,则返回匹配结果的第args个子串
regex split_sub_reg("(\\w*)@(\\w*).(\\w*)");
pos = sregex_token_iterator(str.begin(), str.end(), split_sub_reg, 1);
while(pos != sregex_token_iterator())
{
cout << "[" << *pos << "]" ;
++pos;
}
cout << endl;
//匹配并指定输出顺序
//从下面字符串中提取日期,并转换成 年月日 的顺序输出
std::string input("01/02/2003 blahblah 04/23/1999 blahblah 11/13/1981");
regex re("(\\d{2})/(\\d{2})/(\\d{4})"); // find a date
int const sub_matches[] = { 3, 1, 2 }; // year,month, day
sregex_token_iterator begin( input.begin(), input.end(), re, sub_matches ), end;
// write all the words to std::cout
std::ostream_iterator< std::string > out_iter( std::cout, "\n" );
std::copy( begin, end, out_iter );
}
(四)boost库之正则表达式regex的更多相关文章
- boost库学习之regex
一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...
- boost 正则表达式 regex
boost 正则表达式 regex 环境安装 如果在引用boost regex出现连接错误,但是引用其他的库却没有这个错误,这是因为对于boost来说,是免编译的,但是,正则这个库 是需要单独编译 ...
- VS2008下直接安装使用Boost库1.46.1版本
Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C++ ...
- Boost库安装理解
Boost安装的安装,以及在VS2013下的使用 1. 为什么要安装? boost是一个开源库,因为开源库可以跨平台,可以通过在不同的“硬件”平台上.所以需要安装的操作. 安装,然后编译生成“静态链接 ...
- boost库在windows下的编译和使用
因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boos ...
- Boost库
2014-08-31 Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成 ...
- (九)boost库之文件处理filesystem
(九)boost库之文件处理filesystem filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能 ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
- VS2008编译boost库
一.下载首先从boost官方主页http://www.boost.org/下载最新版boost安装包,我用的版本是boost.1.49.0二.新建文件夹 如果是使用下载的安装包,那么请将boost安装 ...
随机推荐
- javacc
http://www.cnblogs.com/Gavin_Liu/archive/2009/03/07/1405029.html https://javacc.java.net/ http://www ...
- NFC应用(三)点对点(P2P)通信
点对点(P2P)模式允许两个NFC设备之间建立通信链接并交换数据,与读写器.卡模式不一样的就是,P2P模式下数据交互是双向的. P2P遵循ISO18092规范,建立链接后使用NDEF(NFC Data ...
- Qt wrappers for OS X Cocoa widgets
Qt wrappers for OS X Cocoa widgetshttps://github.com/MikeMcQuaid/Qocoa
- 并行编译加快VS C++项目的编译速度
最近编译的项目都比较大,话说自己的电脑配置还行,但编译所花的时间还是很长,遇到需要重新编译整个项目的时候真的有回宿舍睡一觉的冲动.昨天一不小心被我发现了一款软件Xoreax IncrediBuild ...
- javascript之Function函数
在javascript里,函数是可以嵌套的. 如: function(){ funcrion square(x){ return x*x; } return square(10); } 在javas ...
- 转:SCHEME 语言是怎么来的 -1
导言 Scheme 是 LISP 的一个方言(dialect).著名的 SICP 书就是以 Scheme 为教学语言(实际上 SICP 的作者就是 Scheme 的作者). 虽然 Scheme 本身只 ...
- centos单用户模式:修改ROOT密码和grub加密
centos单用户模式:修改ROOT密码和grub加密 CentOSLinux网络应用配置管理应用服务器 Linux 系统处于正常状态时,服务器主机开机(或重新启动)后,能够由系统引导器程序自动引导 ...
- 格而知之4:寻找EXC_BAD_ACCESS
EXC_BAD_ACCESS算是一个比较常见的错误,大部分情况下,它出现在某个对象还未初始化或已被释放后,还去试图访问这个对象的时候,即是在出现悬挂指针的时候(当然也有非悬挂指针导致的EXC_BAD_ ...
- Google Go 语言从入门到应用必备开源项目
Go 语言于 2009 年 11 月正式宣布推出,成为开放源代码项目,发展至今已经具有越来越广泛的影响力,今年更是在 TIOBE 编程语言排行榜中跻身 20 强.很多开发者也逐渐将目光投向这门语言,本 ...
- atitit.导出excel的设计----查询结果 导出为excel的实现java .net php 总结
atitit.导出excel的设计----查询结果 导出为excel的实现java .net php 总结 1. 基本的流程 查询获得list 读取jsp的table获得标题and 字段的map to ...