c++ 11 正则表达式

常用的方法 regex_match regex_search regex_replace 等.

regex_match 要求正则表达式必须与模式串完全匹配,例如:

string str = "o ";
regex pattern("o\\s"); bool matched = regex_match(str,pattern);
if (matched) {
cout << "matched.." << endl;
}else{
cout << "not matched." << endl;
}

上面就可以匹配.如果修改一下下:

string str = "o 1";
regex pattern("o\\s"); bool matched = regex_match(str,pattern);
if (matched) {
cout << "matched.." << endl;
}else{
cout << "not matched." << endl;
}

就是 not matched. 了

regex_match 包含子模式的匹配,并且显示匹配结果,主要使用了

match_results<string::const_iterator> result

存储匹配后的结果.

    string str = "1:2:33:344";
regex pattern("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})"); match_results<string::const_iterator> result;
bool matched = regex_match(str,result,pattern);
if (matched) {
cout << "matched.." << endl;
printf("result.size = %d\n",(int)result.size());
for (int i = 0; i < result.size(); ++i) {
printf("result[%d]:%s\n",i,result[i].str().c_str());
}
}else{
cout << "not matched." << endl;
}

输出:


matched..


result.size = 5


result[0]:1:2:33:344


result[1]:1


result[2]:2


result[3]:33


result[4]:344


主意打印的结果result[0]

regex_search 只要求存在匹配项就可以.

string str = "o 1";
regex pattern("\\w\\s"); bool matched = regex_search(str,pattern);
if (matched) {
cout << "matched.." << endl;
}else{
cout << "not matched." << endl;
}

输出: matched..

如果只想输出第一个匹配项,使用 smatch 存储结果

string str = "o 1s;23235;t;dsf;sd 66 ";
regex pattern(";"); smatch result;
bool matched = regex_search(str,result,pattern);
if (matched) {
cout << "matched.." << endl;
cout << "result.size:" << result.size() << endl;
for (int i = 0; i < result.size(); ++i) {
printf("result[%d]:\t%s\n",i,result[i].str().c_str());
}
}else{
cout << "not matched." << endl;
}

输出结果为:

matched..

result.size:1

result[0]: ;

如果想输出所有的匹配结果,则需要使用 sregex_token_iterator

string str = "o 1s 23235 t ds f sd 66 ";
regex pattern("\\w\\s"); sregex_token_iterator end;
sregex_token_iterator start(str.begin(),str.end(),pattern); while (start != end) {
cout << (*start) << endl;
++start;
}

输出为:

o

s

5

t

s

f

d

6

regex_replace 方法替换掉匹配上的

string str = "o 1s 23235 t ds f sd 66 ";
regex pattern("\\w\\s"); bool matched = regex_search(str,pattern);
if (matched) {
cout << "matched ." << endl;
auto newStr = regex_replace(str,pattern,"---");
printf("newStr : %s\n",newStr.c_str());
}else{
cout << "not matched ." << endl;
}

输出结果为:

matched .

newStr : ---1---2323------d------s---6---

上面就是c++11 正则表达式的基本使用.

内容基本来自于: 这个作者

c++11 正则表达式基本使用的更多相关文章

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

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

  2. 【正则表达式1】C++11正则表达式

    https://www.cnblogs.com/pukaifei/p/5546968.html [正则表达式1]C++11正则表达式   头文件 #include <regex> rege ...

  3. python进阶11 正则表达式

    python进阶11 正则表达式 一.概念 #正则表达式主要解决什么问题? #1.判断一个字符串是否匹配给定的格式,判断用户提交的又想的格式是否正确 #2.从一个字符串中按指定格式提取信息,抓取页面中 ...

  4. 理解C++11正则表达式(2)

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

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

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

  6. C++11 正则表达式——基础知识介绍

    C++11开始支持正则表达式,使得处理文本更加简洁方便.C++11 支持六种正则表达式语法:ECMAScript, basic(POSIX Basic Regular Expressions), ex ...

  7. C++11 正则表达式简单运用

    正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex.regexp.RE.regexps.regexes.regexen. 正则表达式是一种 ...

  8. C++11 正则表达式——实例系统(转载)

    一.用正则表达式判断邮箱格式是否正确 1 #include <regex> #include <iostream> #include <string> bool i ...

  9. C++11正则表达式初探

    C++正则表达式 在此之前都没有了解过C++的正则,不过现在大多数赛事都支持C++11了,因此有必要学习一下,用于快速A签到题. 所在头文件 #include<regex> 正则表达式语法 ...

随机推荐

  1. zpf 获取表单等数据的用法

    2015年4月12日 12:25:35 星期日 zpf框架中获取表单数据的方法 //获得get,post,url中的数据 private function setData() { $this-> ...

  2. Kafka单机环境部署

    前面说过Kafka集群环境的部署,现在主要说一下在本地测试中Kafka单机环境的部署,和前面一样首先保证zookeeper服务的正常运行,然后解压并释放kafka安装包,并放到指定位置: tar -x ...

  3. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  4. 8.js模式-状态模式

    1. 状态模式 var offLightState = function(light){ this.light = light; } offLightState.prototype.buttonWas ...

  5. 关于MySQL count(distinct) 逻辑的一个bug【转】

    本文来自:http://dinglin.iteye.com/blog/1976026#comments 背景 客户报告了一个count(distinct)语句返回结果错误,实际结果存在值,但是用cou ...

  6. cairo-1.14.6 static compiler msys mingw32

    gtk2.x 静态编译时 需要注意的是 cairo cairo 1.14.x 使用了 mutex , 用动态方式时 DllMain 中调用了 CAIRO_MUTEX_INITIALIZE () 在静态 ...

  7. cmd导入oracle数据

    ctrl+r 输入imp

  8. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 51nod 1070 Bash游戏 V4 (斐波那契博弈)

    题目:传送门. 有一堆个数为n(n>=2)的石子,游戏双方轮流取石子,规则如下: 1)先手不能在第一次把所有的石子取完,至少取1颗: 2)之后每次可以取的石子数至少为1,至多为对手刚取的石子数的 ...

  10. [Ubuntu] ubuntu10.04系统维护之Wine的安装

    在介绍安装wine之前,我想是有必要先介绍一下Wine的.当然,如果是Liunx的高手,我想是没必要看的,但是对于笔者这样的菜鸟级人物还是需要看一下的. Wine是一款Liunx下的模拟器软件,但是W ...