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. php发送post请求到nodejs服务器

    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); 改为 curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build ...

  2. Multi-level Multi-select plugin

    Property file: Country[tab]City United States[tab]San Francisco United States[tab]Chicago Mexico[tab ...

  3. sql cross join table

    A 表数据如下图所示 B表数据如下图所示

  4. angularJs自定义指令.directive==类似自定义标签

    创建自定义的指令 除了 AngularJS 内置的指令外,我们还可以创建自定义指令. 你可以使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名 ...

  5. LeetCode Graph Valid Tree

    原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...

  6. http.Handler 与Go的错误处理

    原文地址    在之前我写过一篇关于通过使用http.HandlerFunc来实现一个定制handler类型用来避免一些平常的错误的文章.func MyHandler(w http.ResponseW ...

  7. 三维高斯模型 opencv实现

    OnProbabilityModel() { int i; for(int x=0;x<workImg->height;x++) { for(int y=0;y<workImg-&g ...

  8. google bookmarket api

    引用: 最近做了google书签同步的模块,发现google并没有公开bookmark相关的api接口,在网上也找了些资料,通过自己抓包分析,测试,总结下使用bookmark接口的心得,我是在andr ...

  9. html,移动端代码

    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale= ...

  10. Windows Azure Azure 简介

    平台介绍 Windows Azure作为一个微软公有云平台,被寄予了厚望. 可以说Windows Azure与Windows RT一样是微软战略转型的重点. 2012年9月微软与中国本土的电信服务提供 ...