c++ 11 正则表达式

常用的方法 regex_match regex_search regex_replace 等.

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

  1. string str = "o ";
  2. regex pattern("o\\s");
  3. bool matched = regex_match(str,pattern);
  4. if (matched) {
  5. cout << "matched.." << endl;
  6. }else{
  7. cout << "not matched." << endl;
  8. }

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

  1. string str = "o 1";
  2. regex pattern("o\\s");
  3. bool matched = regex_match(str,pattern);
  4. if (matched) {
  5. cout << "matched.." << endl;
  6. }else{
  7. cout << "not matched." << endl;
  8. }

就是 not matched. 了

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

  1. match_results<string::const_iterator> result

存储匹配后的结果.

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

输出:


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 只要求存在匹配项就可以.

  1. string str = "o 1";
  2. regex pattern("\\w\\s");
  3. bool matched = regex_search(str,pattern);
  4. if (matched) {
  5. cout << "matched.." << endl;
  6. }else{
  7. cout << "not matched." << endl;
  8. }

输出: matched..

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

  1. string str = "o 1s;23235;t;dsf;sd 66 ";
  2. regex pattern(";");
  3. smatch result;
  4. bool matched = regex_search(str,result,pattern);
  5. if (matched) {
  6. cout << "matched.." << endl;
  7. cout << "result.size:" << result.size() << endl;
  8. for (int i = 0; i < result.size(); ++i) {
  9. printf("result[%d]:\t%s\n",i,result[i].str().c_str());
  10. }
  11. }else{
  12. cout << "not matched." << endl;
  13. }

输出结果为:

matched..

result.size:1

result[0]: ;

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

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

输出为:

o

s

5

t

s

f

d

6

regex_replace 方法替换掉匹配上的

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

输出结果为:

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. QPS计算方法

    2016年3月14日 13:55:39 星期一 好久没写文章了, 神烦.....

  2. spfa(模板)

    spfa作为图论中的常用算法,深受各类出题人和各位OIer的喜爱: so,为了给大众创造福利,宝宝在此奉上spfa大发的思路和模板:以感谢社会, 感谢CCF,感谢CCTV, 感谢我的老师,感谢同学们, ...

  3. Hibernate简单分页

    5.1 准备工作 建立项目,加入jar 建立hibernate.cfg.xml 建立pojo类和对应的映射文件 5.2 建立vo类PageEntity package org.guangsoft.vo ...

  4. iOS开发MAC下配置svn

    版本控制对于团队合作显得尤为重要,那么如何在iOS开发中进行版本控制呢?在今天的博客中将会介绍如何在MAC下配置SVN服务器,如何导入我们的工程,如何在Xcode中进行工程的checkOut和Comm ...

  5. sql 查询表的所有详细信息

    SELECT (case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空 a.colorder as 字段序号, a.nam ...

  6. 杨辉三角用java实现

    代码如下: public class ErArray { public static void main(String[] args) { //杨辉三角 int[][] num = new int[1 ...

  7. VAssistX的VA Snippet Editor的类注释和函数注释

    title:类注释shortcut:=== /******************************************************** [DateTime]:$YEAR$.$M ...

  8. Xcode - 修改变量名、类名及字符串的替换操作

    在做iOS开发代码优化的工作时,优化代码结构之前,我们应该先整理好工程的外貌,将文件和类的命名进行规范,在Xcode中为我们提供了方便而强大的名称修改功能. 第一步:修改类名 将鼠标点击放在类的名称上 ...

  9. JAVA数据库基本操作 (转)

    JAVA数据库基本操作指南   Java数据库操作基本流程:取得数据库连接 - 执行sql语句 - 处理执行结果 - 释放数据库连接. 一.取得数据库连接 1.用DriverManager取数据库连接 ...

  10. Android Programming: Pushing the Limits -- Chapter 4: Android User Experience and Interface Design

    User Stories Android UI Design 附加资源 User Stories: @.通过写故事来设计应用. @.每个故事只关注一件事. @.不同的故事可能使用相同的组件,因此尽早地 ...