C++ Boost/tr1 Regex(正则表达式)快速指南

正则表达式自Boost 1.18推出,目前已经成为C++11(tr1)的标准部分。

本文以Boost 1.39正则表达式为基础,应该广泛适用于其他版本的Boost。对namespace稍加修改,即可适用tr1标准。

0、regex对象

类似于Java中的Pattern,Boost中的正则表达式对象为:

boost::regex

常见构造方法2种:

 
 
1
2
3
4
5
// 1. 直接使用正则表达式的字符串构造。
boost::regex reg1("\\d{18}");
 
// 2. 加入参数regex_constants,这里是忽略大小写case
boost::regex reg2("ok", boost::regex::icase);

1、regex_match

首先要明确match和search的区别:

  • match针对整个字符串,若整个串匹配,match返回true,否则false。
  • search非针对整串,若任意部分子串匹配,search返回true,否则false。

明确了这点之后,来看regex_match的三种常见用法:

(1) 使用string字符串、regex对象直接match。

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <boost/regex.hpp>
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    // Match the whole string
    // regex_match(str, regex)
    boost::regex pat1("(\\d{4}-){3}\\d{4}");
    string card_str("1234-5678-4321-8765");
    cout << boost::regex_match(card_str, pat1) << endl;
    cout << boost::regex_match(card_str, boost::regex("\\d{12}")) << endl;
    cout << "----" << endl;
    return 0;
}

这个简单明了:若card_str全串匹配了,返回1,否则0。

输出如下:

 
 
1
2
3
1
0
----

(2) 使用迭代器替换string,并给regex加入参数

如下所述,我们知道string的头3个、尾3个是垃圾字符,可以用迭代器指明match工作的begin和end位置。注意的是:boost中,不提供string、begin(int)、end(int)这种参数形式。

同时,在构造regex的时候,我们给定了一个参数boost::regex::icase,表示该正则对象将忽略大小写!

 
 
1
2
3
4
5
6
7
    // Match the whole string, define string by iterator, ignore case
    // regex_match(begin, end, regex, flags)
    string card_str2("|||1234-5678-4321-8765OK|||");
    boost::regex pat2("(\\d{4}-){3}\\d{4}ok", boost::regex::icase);
    cout << boost::regex_match(card_str2.begin()+3, card_str2.end()-3, pat2) << endl;
    cout << boost::regex_match(card_str2.begin()+3, card_str2.end()-3, boost::regex("(\\d{4}-){3}\\d{4}ok")) << endl; // Case wrong
    cout << "----" << endl;

输出如下:

 
 
1
2
3
1
0
----

(3) match,并返回分组的match result

我们都知道,正则中的括号表示分组,例如:

字符串和正则:

 
 
1
2
boost::regex pat3("(\\d{4})-(\\d{4})-(\\d{4})-(\\d{4})");
string card_str3("1234-5678-4321-8765");

pat3中有4个分组,则regex_match后,match result会形成5个分组,0为全串,1~4分别为1234、5678、4321、8765。

好了,看下用法吧:

 
 
1
2
3
4
5
6
7
8
9
10
11
    // Match the whole string, return the match part
    boost::regex pat3("(\\d{4})-(\\d{4})-(\\d{4})-(\\d{4})");
    string card_str3("1234-5678-4321-8765");
    boost::smatch mat3;
    cout << boost::regex_match(card_str3, mat3, pat3) << endl;
    cout << mat3.size() << endl;
    for(size_t i=0; i<mat3.size(); i++)
    {
        cout << "Match " << i << ":" << mat3[i].str() << endl;
    }
    cout << "----" << endl;

输出如下:

 
 
1
2
3
4
5
6
7
8
1
5
Match 0:1234-5678-4321-8765
Match 1:1234
Match 2:5678
Match 3:4321
Match 4:8765
----

这里要再多解释一下,smatch是match_result的模版特例化类型,特别针对std::string的:

 
 
1
2
3
4
5
6
7
8
9
class match_results;
 
typedef match_results<const char*> cmatch;
 
typedef match_results<const wchar_t*> wcmatch;
 
typedef match_results<string::const_iterator> smatch;
 
typedef match_results<wstring::const_iterator> wsmatch;

 2、regex_search

下面考虑对字符串任意部分匹配的regex_search。

转:C++ Boost/tr1 Regex(正则表达式)快速指南的更多相关文章

  1. (四)boost库之正则表达式regex

    (四)boost库之正则表达式regex 正则表达式可以为我们带来极大的方便,有了它,再也不用为此烦恼 头文件: #include <boost/regex.hpp> 1.完全匹配 std ...

  2. [译] MongoDB Java异步驱动快速指南

    导读 mongodb-java-driver是mongodb的Java驱动项目. 本文是对MongoDB-java-driver官方文档 MongoDB Async Driver Quick Tour ...

  3. Visual Studio使用正则表达式快速统计总共代码行数

    原文:Visual Studio使用正则表达式快速统计总共代码行数 按CTRL+SHIFT+F,勾上支持正则表达式,然后输入搜索内容: <span style="font-family ...

  4. (译)快速指南:用UIViewPropertyAnimator做动画

    翻译自:QUICK GUIDE: ANIMATIONS WITH UIVIEWPROPERTYANIMATOR 译者:Haley_Wong iOS 10 带来了一大票有意思的新特性,像 UIViewP ...

  5. JUnit5 快速指南

    JUnit5 快速指南 version: junit5 1. 安装 2. JUnit 注解 3. 编写单元测试 3.1. 基本的单元测试类和方法 3.2. 定制测试类和方法的显示名称 3.3. 断言( ...

  6. 【SFA官方翻译】使用 Kubernetes、Spring Boot 2.0 和 Docker 的微服务快速指南

    [SFA官方翻译]使用 Kubernetes.Spring Boot 2.0 和 Docker 的微服务快速指南 原创: Darren Luo SpringForAll社区 今天 原文链接:https ...

  7. Emacs 快速指南(中文翻译)

      Emacs 快速指南 目录 1. 小结(SUMMARY) 2. 基本的光标控制(BASIC CURSOR CONTROL) 3. 如果 EMACS 失去响应(IF EMACS STOPS RESP ...

  8. 29 A Quick Guide to Go's Assembler 快速指南汇编程序:使用go语言的汇编器简介

    A Quick Guide to Go's Assembler 快速指南汇编程序:使用go语言的汇编器简介 A Quick Guide to Go's Assembler Constants Symb ...

  9. Emacs 快速指南 - 原生中文手册

    Emacs 快速指南 -折叠目录 1. 小结(SUMMARY) 2. 基本的光标控制(BASIC CURSOR CONTROL) 3. 如果 EMACS 失去响应(IF EMACS STOPS RES ...

随机推荐

  1. javascript小实例,多种方法实现数组去重问题

    废话不多说,直接拿干货! 先说说这个实例的要求:写一个方法实现数组的去重.(要求:执行方法,传递一个数组,返回去重后的新数组,原数组不变,实现过程中只能用一层循环,双层嵌套循环也可写,只做参考): 先 ...

  2. Altium Designer 文档信息设置以及模板制作

    原理图文档模板制作方法一.在DXP原理图设计环境下,新建一个自由原理图文档.单击:文件→新建→原理图,或者使用快捷键Ctrl+N打开Files资源面板,在“新建”项目下的选择“Schematic Sh ...

  3. syslog-ng日志系统

    一.基础syslog-ng作为syslog的替代工具,可以完全替代syslog的服务,并且通过定义规则,实现更好的过滤功能.系统自带版本(我的是红旗,不同系统用不同的方式查询): 引用 # rpm - ...

  4. k8s入门系列之扩展组件(二)kube-ui安装篇

    kube-ui是k8s提供的web管理界面,可以展示节点的内存.CPU.磁盘.Pod.RC.SVC等信息. 1.编辑kube-dashboard-rc.yml定义文件[root@master kube ...

  5. [BS-28] iOS中分页的几种算法

    iOS中分页的几种算法 总记录数:totalRecord 每页最大记录数:maxResult 算法一: totalPage = totalRecord % maxResult == 0 ? total ...

  6. Solr6.2.0 + zookeeper 集群配置

    zookeeper1 : 192.168.1.103zookeeper2 : 192.168.1.104zookeeper3 : 192.168.1.105solr1 : 192.168.1.106s ...

  7. css3渐变色彩

    CSS3 Gradient 分为线性渐变(linear)和径向渐变(radial).由于不同的渲染引擎实现渐变的语法不同,这里我们只针对线性渐变的 W3C 标准语法来分析其用法,其余大家可以查阅相关资 ...

  8. RFC-2068-http

    本文档规定了互联网社区的标准组协议,并需要讨论和建议以便更加完善.请参考 “互联网官方协议标准”(STD 1)来了解本协议的标准化状态.本协议不限流传发布. 版权声明 Copyright (C) Th ...

  9. VMware下安装虚拟机Ubuntu14.04 Server设置桥接方式

    我本地的采用的上网方式的拨号上网,IP段是一公网下的通过路由设置的局域网,网段182.18.1.* 本地连接包含以下: 其中无线上网卡的.WMware桥接是自定义的局域网IP段:192.168.253 ...

  10. 反射调用方法时的两种情况,走get set和不走get set

    @Test public void test1() throws Exception{  //获取User类  Class class1=Class.forName("cn.jbit.bea ...