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. List<T> 添加 DataTable

    public System.Data.DataTable getDataTable() { System.Data.DataTable dt = new System.Data.DataTable() ...

  2. iOS:搭建本地的服务器

    一.介绍 作为一个专业的程序员,不管你是前端还是移动端或者是后台,能够自己试着搭建一个本地的服务器还是很有必要的,有的时候,我们可以自己测试一些数据,很方便开发.其实,mac是自带有本地的服务器的,用 ...

  3. Progressive Scanning (逐行扫描) vs Interlaced Scanning (隔行扫描)

    source: http://sh.sina.com.cn/20041207/231443445.shtml 逐行扫描每一帧图像均是由电子束顺序地一行接着一行连续扫描而成.要得到稳定的逐行扫描图像,每 ...

  4. App软件开发的10个常用技巧

    移动应用市场用户争夺战日益激烈,原来做APP拼想法拼创意拼是否抓住用户痛点.现在,精细化用户体验成为了一个APP能否留存用户的关键问题,一旦用户觉得体验不畅,马上就有竞品APP后补,如何开发高性能的移 ...

  5. 关于HTML中标签<a>使用js的注意事项

    以下两点都不可取: 1.<a href="#" onClick="popUp('http://www.baidu.com');return false;" ...

  6. python - 文件迭代

    >>> f=open('passwd','r')>>> for lines in f:... print lines >>> f=open('pa ...

  7. linux 下查看系统资源和负载,以及性能监控

    1,查看磁盘   df -h   2,查看内存大小   free   free [-m|g]按MB,GB显示内存   vmstat   3,查看cpu   cat /proc/cpuinfo   只看 ...

  8. Java tomcat启动失败(Servlet3.0 Web Project):A child container failed during start

    Tomcat启动失败,失败全部信息: 五月 , :: 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetProper ...

  9. mssql手工注入及绕过术

      报错注入: - 例子:http://www.kfgtfcj.xxx.cn/lzygg/Zixun_show.aspx?id=1 [1]首先爆版本:http://www.kfgtfcj.xxx.cn ...

  10. Git 遇到了 early EOF index-pack failed 问题

    Git 遇到了 early EOF index-pack failed 问题 今天想 clone 一下 boost 在 github 的 repo,结果在 clone 的过程中遇到了下面的错误.我原本 ...