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. ACM/ICPC 之 分治法入门(画图模拟:POJ 2083)

    题意:大致就是要求画出这个有规律的Fractal图形了= = 例如 1 对应 X 2 对应 X  X   X    X  X 这个题是个理解分治法很典型的例子(详情请参见Code) 分治法:不断缩小规 ...

  2. TS初探

    简介 TypeScript具有类型系统,且是JavaScript的超集.它可以编译成普通的JavaScript代码. TypeScript支持任意浏览器,任意环境,任意系统并且是开源的.Ts主要用于解 ...

  3. NHibernate实战详解(二)映射配置与应用

    关于NHibernate的资料本身就不多,中文的就更少了,好在有一些翻译文章含金量很高,另外NHibernate与Hibernate的使用方式可谓神似,所以也有不少经验可以去参考Hibernate. ...

  4. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  5. codeforces 495A. Digital Counter 解题报告

    题目链接:http://codeforces.com/problemset/problem/495/A 这个题目意思好绕好绕~~好绕~~~~~,昨天早上做得 virtual 看不懂,晚上继续看还是,差 ...

  6. C#中XmlTextWriter读写xml文件详细介绍(转)

    转自http://www.jb51.net/article/35230.htm   .NET中包含了很多支持XML的类,这些类使得程序员使用XML编程就如同理解XML文件一样简单.在这篇文章中,我将给 ...

  7. LeetCode 326 Power of Three

    Problem: Given an integer, write a function to determine if it is a power of three. Could you do it ...

  8. centos7 php7 httpd

    安装php之前,要先安装几个 1.下载php源码:http://cn2.php.net/distributions/php-7.0.6.tar.gz. 2.然后使用命令:tar -zxvf php-7 ...

  9. ASCII 非打印字符

    项目出了问题,因为AscII非打印字符的原因,后来找了一下啊ASCII的非打印字符,总共有31个,然后我们直接全部替换成问号了. 解决方式为先找到非打印字符,这是我从网上找的非打印字符表: 进制 十六 ...

  10. Swift - @IBDesignable和@IBInspectable

    前言: 用storyboard/xib搞项目时,一些属性在Interface Builder上时无法设置,比如常用的layer的一些属性cornerRadius,borderColor等 (有时没必须 ...