今天有幸(2016/3/19)在上海参加了C++交流会,见到了梦寐已久想见的台湾C++大神老师侯捷,心情十分的激动。侯老师对C++理解的深刻,让人叹为观止。以为他教学的严谨,说话方式娓娓道来,听着非常舒服。末尾附上一张侯老师照片。

  我们接着上文介绍C++11的正则表达式。本节将接着上文遗留问题开始展开,并且将结合网上的一些优秀的博客。

正文

 C++11 支持六种正则表达式语法:

 ECMAScript,

 basic(POSIX Basic Regular Expressions),

extended(POSIX Extended Regular Expressions ),

 awk(POSIX awk) ,

grep(POSIX grep ),

egrep(POSIX grep –E)。其中ECMAScript最为强大。

首先,我们将介绍正则表达式一些通用的基本类型。

1 basic_regex

//basic_regex: 这是一个包含一个正则表达式的模板类。通常有两种特化方式:
a) typedef basic_regex<char> regex;
b) typedef basic_regex<wchar_t> wregex;

2 match_results:

 这个类包含了与给定正则表达式匹配的序列。当empty()成员返回true或者size()成员返回0,表明没有找到匹配项。
否则,当empty()返回false,size()返回值>=1 表明发生了匹配。

//match_results有如下特化方式:
a) typedef match_results<const char*> cmatch;
b) typedef match_results<const wchar_t*> wcmatch;
c) typedef match_results<string::const_iterator> smatch;
d) typedef match_results<wstring::const_iterator> wsmatch;
match[0]:代表整个匹配序列 ;
match[1]:代表第一个匹配子序列;
match[2]: 代表第二个匹配子序列,以此类推。

特别需要注意的是:正则表达式的编写过程中,需要达到上面match[1][2][3]效果的话,必须将每个match数据通过捕获组(capture group)的方式用括号括起来(.*),例如:

regex reg("<(.*)>(.*)</(\\1)>")

如果你把括号删除,显然是没有上面说的那种效果的。其中\1指的是第一个捕获组,\2 当然就是指代第二个啦。

3 sub_match: 该模板类用来表示与一个已标记的子表达式匹配的序列。这个匹配是通过一个迭代器对来表示的,该迭代器对表明了已匹配的正则表达式的一个范围。可以特化为下面几种情况:

a)    typedef sub_match<const char*>              csub_match;
b) typedef sub_match<const wchar_t*> wcsub_match;
c) typedef sub_match<string::const_iterator> ssub_match;
d) typedef sub_match<wstring::const_iterator> wssub_match;

4 迭代器介绍:正则表达式迭代器用来遍历这个正则表达式序列,通过一个迭代器区间来表示匹配的区间。

1. regex_iterator:
a)typedef regex_iterator<const char*> cregex_iterator;
b)typedef regex_iterator<const wchar_t*> wcregex_iterator;
c)typedef regex_iterator<string::const_iterator> sregex_iterator;
d)typedef regex_iterator<wstring::const_iterator> wsregex_iterator; 2. regex_token_iterator:
a) typedef regex_token_iterator<const char*> cregex_token_iterator;
b) typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
c) typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
d) typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;

一些test代码:

     string data = "XML tag: <tag-name>the value</tag-name>.";
cout << "data: " << data << endl << endl;
smatch m;
bool found = regex_search(data, m, regex("<(.*)>(.*)</(\\1)>"));
cout << "m.empty() " << boolalpha << m.empty() << endl;
cout << "m.size() " << m.size() << endl;
if (found)
{
cout << "m.str() " << m.str() << endl;
cout << "m.length() " << m.length() << endl;
cout << "m.position() " << m.position() << endl;
cout << "m.prefix().str() " << m.prefix().str() << endl;
cout << "m.suffix().str() " << m.suffix().str() << endl;
}
for (int i = 0; i < m.size(); i++)
{
cout << "m[" << i << "].size():" << m[i].str() << endl;
cout << "m.str(" << i << "):" << m.str(i) << endl;
cout << "m.position(" << i << "):" << m.position(i) << endl;
}
cout << "match: " << endl;
for (auto pos = m.begin(); pos != m.end(); pos++)
{
cout << " " << *pos << " ";
cout << "(length" << pos->length() << ")" << endl;
}
     //第二个例子
data = "<person>\n"
" <first>Nico</first>\n"
" <last>Josuttis</last>\n"
"</person>\n";
auto pos = data.cbegin();
auto end = data.cend();
regex reg("<(.*)>(.*)</(\\1)>"); for (;regex_search(pos, end, m, reg); pos = m.suffix().first)
{
cout << "match: " << m.str() << endl;
cout << "tag: " << m.str(1) << endl;
cout << "value: " << m.str(2) << endl;
}

  我们要查找的字符串是这样的格式:

  另外,第二个例子中将字符串添加换行符,如果没有添加,将无法辨别<first><last>,最后只能找到<person>。

Regex Iterator:  采用迭代器的方式进行访问或通过for_each()

sregex_iterator pos(data.cbegin(),data.cend(),reg);
sregex_iterator end; for(; pos!=end; ++pos)
{
cout << "match " << pos -> str() << endl;
cout << "tag:" << pos -> str(1) << endl;
cout << "value:" << pos -> str(2) << endl;
} for_each(beg,end,[](const smatch &m){
cout << "match " << pos -> str() << endl;
cout << "tag:" << pos -> str(1) << endl;
cout << "value:" << pos -> str(2) << endl;
});

Regex Token Iterator

1、regex reg("<(.*)>(.*)</(\\1)>");

sregex_token_iterator pos(data.cbegin(),data.cend(),reg,{0,2});//0代表了获取整个match,2代表获取第二个group;-1代表了想知道所有子序列。

2、regex  sep("[[:space:]]*[;,.][[:space:]]*");//以;.,分割。

sregex_token_iterator pos(data.cbegin(),data.cend(),sep,-1);//得到所有子序列。(具体用法请参考c++11标准库,有详细用法和介绍)

小结:

前面2节主要介绍两个函数(regex_search regex_match),但是这两个函数不能定义出在那个位置,我们引入了迭代器、group的概念。下一节主要介绍替换、regex常量以开头说的6种正则表达式语法,最后想通过几个简单的例子练习一下。其实标准库里面说的很清楚,如果不明白的话,建议去买一本侯捷翻译的c++11标准库。

理解C++11正则表达式(2)的更多相关文章

  1. 理解c++11正则表达式 (1)

    概要 C++11提出了正则表达式这个概念,只需在头文件中包含#include<regex>即可.我们可以完成: Match 将整个输入拿来比对匹配某个正则表达式 Search 查找与正则表 ...

  2. C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)

    因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...

  3. c++11 正则表达式基本使用

    c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...

  4. C++11 | 正则表达式(4)

    C++11还支持正则表达式里的子表达式(也叫分组),用sub_match这个类就行了. 举个简单的例子,比如有个字符串"/id:12345/ts:987697413/user:678254& ...

  5. 深入理解C++11【5】

    [深入理解C++11[5]] 1.原子操作与C++11原子类型 C++98 中的原子操作.mutex.pthread: #include<pthread.h> #include <i ...

  6. 深入理解C++11【4】

    [深入理解C++11[4]] 1.基于范围的 for 循环 C++98 中需要告诉编译器循环体界面范围.如for,或stl 中的for_each: int main() { ] = { , , , , ...

  7. 深入理解C++11【3】

    [深入理解C++11[3]] 1.POD类型 Plain Old Data. Plain 表示 了POD是个普通的类型.C++11将POD划分为两个基本概念的合集: 1)平凡的(trivial) 2) ...

  8. 深入理解C++11【2】

    [深入理解C++11[2]] 1.继承构造函数. 当基类拥有多个构造函数的时候,子类不得不一一实现. C++98 可以使用 using 来使用基类的成员函数. #include < iostre ...

  9. 深入理解C++11

    [深入理解C++11] 1.很多 现实 的 编译器 都 支持 C99 标准 中的__ func__ 预定 义 标识符 功能, 其 基本 功能 就是 返回 所在 函数 的 名字. 编译器 会 隐式 地 ...

随机推荐

  1. Gradle实战教程之依赖管理

    这是从我个人网站中复制过来的,原文地址:http://coolshell.info/blog/2015/05/gradle-dependency-management.html,转载请注明出处. 简要 ...

  2. 页面mask css

    <html> <head> <style type="text/css"> .share_mask { position: fixed; top ...

  3. mysql数据库导出时报错mysqldump: Got error: 145的解决方法

      在给mysql数据库备份时,报错:mysqldump: Got error: 145: Table './jxzhtopenfire/ofoffline' is marked as crashed ...

  4. linux-信号。

    信号 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的. 信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知道信号到 ...

  5. http 常用状态码及含义

    http://www.kuaipan.cn/developers/document_status.htm

  6. CocoaPods安装和使用及问题:Setting up CocoaPods master repo-b

    目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaPods? 场景1:利用CocoaPods,在项目中导入AFNetworking类库 场景2:如何正确编译运行一 ...

  7. PPT2010小技巧 教你如何快捷抠图

    相信不少人在做PPT时,都有想插入个漂亮个性图案的想法,但是往往手头上的图片都不太令人满意,需要“裁剪”一下才能达不到自己想要的效果.这时大部分人可能会PS,但是相比起今天要分享给大家的方法,步骤就显 ...

  8. ORACLE 定时任务JOB

    http://www.cnblogs.com/xclw/archive/2009/12/04/1616945.html

  9. [原博客] POJ 2975 Nim 统计必胜走法个数

    题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...

  10. 解决iOS应用内购买报错:invalidProductIdentifiers

    当写完IAP业务过程后,点击测试却发现没有返回成功的商品Id,反而返回了无效的商品:response.invalidProductIdentifiers 这种情况下考虑以下因素: 创建的App ID是 ...