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特效(调试笔记)

    JavaScript特效 一.在网页上显示当前的时间日期,例如:“2016年3月26日 星期六”. js源代码: function getTime() { var today = new Date() ...

  2. Java学习-048-插件应用之 Find Bugs

    FindBugs 是一个静态分析工具,它可以检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题,使用 FindBugs 可以在不实际运行程序的情况对软件进行分析.使用时最好将字节 ...

  3. LeetCode Strobogrammatic Number II

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...

  4. AngularJs转换json日期/Date(00000)/

    //过滤器ngApp.filter('jsonDate', function ($filter) { return function (input, format) { var timestamp = ...

  5. linux command screen

    喜欢这句话 “我们永远相信,分享是一种美德 | We Believe, Great People Share Knowledge...” 但是这种美德不是为什么都要问的人准备的 ——————————— ...

  6. c++获取系统时间(引用别人的博文)

    //方案— 优点:仅使用C标准库:缺点:只能精确到秒级#include <time.h> #include <stdio.h> int main( void ) {     t ...

  7. GridView布局,自定义适配器,水平滚动

    添加GridItem布局XML文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  8. Yeoman自动构建js项目

    Aug 19, 2013 Tags: bowergruntJavascriptjsnodejsyeomanyo Comments: 10 Comments Yeoman自动构建js项目 从零开始nod ...

  9. 使用cygwin出现syntax error near unexpected token'$'do\r

    直接从csdn复制粘贴的.sh代码,放到cygwin下运行sh的时候出错syntax error near unexpected token'$'do\r 解决方法: 1.下载notepad++ 2. ...

  10. ClippingNode实现新手引导高亮裁切

    ClippingNode的使用 概述 ClippingNode(裁剪节点)可以用来对节点进行裁剪,可以根据一个模板切割图片的节点,生成任何形状的节点显示. ClippingNode是Node的子类,可 ...