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. SPARK支持的常见文件格式

    SequenceFile读写文件Scala\java类型对应表

  2. android.util.Base64结尾加\n的问题

    测试代码,String data,String key. SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ ...

  3. ruby 分析日志,提取特定记录

    读取日志中的每一行,分析后存入hash,然后做累加 adx_openx=Hash.new(0) File.open('watch.log.2016-08-24-21').each do |line| ...

  4. SimpleInjector的使用

    SimpleInjector的使用       国庆大假,但是,小弟依然学习,前天去看了房交会,尼玛,吓屎宝宝了,还是安静的坐在公司里巧代码比较合适: the usage of injector co ...

  5. Unit01-OOP-对象和类(上)

    Unit01-OOP-对象和类(上) 1.什么是类?什么是对象?  1)现实生活是由很多很多对象组成的    基于对象抽出了类  2)对象:真实存在的单个的个体    类:类型.类别,代表一类个体  ...

  6. Android内存泄露

    Android 内存泄漏是一个十分头疼的事情.LeakCanary是一款开源软件,主要作用是检测 Android APP 内存泄露.比起以前的 MAT 工具,LeakCanary 有着十分强大的功能, ...

  7. SQL调优

    # 问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...

  8. HTML两张图片叠加问题的进一步修改

    要想两张图片叠加,只需在circle添加一个Position:absolute就OK了 以上几步,很多大侠前辈都已经说过,相信不用再啰嗦,我想说的是一种扩展,将示例放到右边,可能因为我悟性低,研究了一 ...

  9. 查看ADOP会话

    查看ADOP有哪些会话: $ adop -status Enter the APPS username: apps Enter the APPS password: Current Patching ...

  10. JavaScript挑战复杂报表——1总述

    今天用自己写的库完成了一个40列填报报表的前后台调试,所花费的时间超过预期很多.遇到的坑有:ajax回调函数写错导致循环调用,没有考虑到java的request.getParameter()方法读入数 ...