#include <string>
#include <regex>
#include <iostream>
using namespace std;int testRegexSearch()
{//定义正则表达式,匹配时间格式regex testRegex("[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}");//要匹配的字符串string strText("OTA LOG SFTCH/MPA Stream 2/Reservation Accept  07:23:50.580 Channel: 147, Pilot PN: 232");//结果:cmatch只能存储一个结果cmatch result; //search 是匹配子字符串, match 是匹配整个字符串if (regex_search(strText.c_str(), result, testRegex, regex_constants::format_default)){cout << result.str() << endl;}else{cout << "fail." << endl;}return 0;
}bool test_email_valid(const std::string& email)
{const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");bool isValid = std::regex_match(email, pattern);std::cout << email << " : " << (isValid ? "valid" : "invalid") << std::endl;return isValid;
}int testRegexMatch()
{std::string email1 = "marius.bancila@domain.com";    std::string email2 = "mariusbancila@domain.com";    std::string email3 = "marius_b@domain.co.uk";    std::string email4 = "marius@domain";       test_email_valid(email1);test_email_valid(email2);test_email_valid(email3);test_email_valid(email4);    return 0;
}void show_ip_parts(const std::string& ip)
{// IP格式const std::regex pattern("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");    // object that will contain the sequence of sub-matches    std:: match_results<std::string::const_iterator> result;// match the IP address with the regular expression    bool valid = std:: regex_match(ip, result, pattern);    std::cout << ip << " \t: " << (valid ? "valid" : "invalid") << std::endl;// if the IP address matched the regex, then print the parts    if(valid)        {        std::cout << "b1: " << result[1] << std::endl;        std::cout << "b2: " << result[2] << std::endl;        std::cout << "b3: " << result[3] << std::endl;        std::cout << "b4: " << result[4] << std::endl;        }
}int testMatchResult()
{    show_ip_parts("1:22:33:444");    show_ip_parts("1:22:33:4444");    show_ip_parts("100:200");           return 0;
}int testMultiSearch()
{    // 星期模式const std::regex pattern("\\w+day");       // 文本 std::string weekend = "Saturday and Sunday, but some Fridays also.";//需要注意一下这里const std::sregex_token_iterator end;for (std::sregex_token_iterator it(weekend.begin(),weekend.end(), pattern); it != end ; ++it){std::cout << *it << std::endl;}std::cout<<std::endl;return 0;
}int regexReplace()
{    // 需要转换的文本std::string text = "This is a element and this a unique ID.";    // regular expression with two capture groupsconst std::regex pattern("(\\ba (a|e|i|u|o))+");// the pattern for the transformation, using the second// capture groupstd::string replace = "an $2";std::string newtext = std::regex_replace(text, pattern, replace);std::cout << newtext << std::endl;std::cout << std::endl;return 0;
}std::string format_date(const std::string& date)
{    // regular expression    const std:: regex pattern("(\\d{1,2})(\\.|-|/)(\\d{1,2})(\\.|-|/)(\\d{4})");// transformation pattern, reverses the position of all capture groupsstd::string replacer = "$5$4$3$2$1";// apply the tranformationreturn std:: regex_replace(date, pattern, replacer);
}int regexReplaceDate()
{    std::string date1 = "1/2/2008";    std::string date2 = "12.08.2008";   std::cout << date1 << " -> " << format_date(date1) << std::endl;    std::cout << date2 << " -> " << format_date(date2) << std::endl;    std::cout << std::endl;    return 0;
}int searchCount() {// "new" and "delete" 出现的次数是否一样?    std::regex reg("(new)|(delete)");    std::smatch m;    std::string s = "Calls to new must be followed by delete. Calling simply new results in a leak!";    int new_counter=0;    int delete_counter=0;    std::string::const_iterator it=s.begin();    std::string::const_iterator end=s.end();    while (std::regex_search(it, end, m, reg)){// 是 new 还是 delete?m[1].matched ? ++new_counter : ++delete_counter;it=m[0].second;}if (new_counter!=delete_counter) {std::cout << "Leak detected!\n";}else {std::cout << "Seems ok...\n";}std::cout << std::endl;return 0;
}int testRegexMain()
{// 1) 搜索时间格式 07:23:50.580 并显示出来cout << ">>>>>>>>>>>>>>testRegexSearch" << endl;testRegexSearch();// 2) 检测email格式是否正确 marius.bancila@domain.comcout << ">>>>>>>>>>>>>>testRegexMatch" << endl;testRegexMatch();// 3) 检测是否符合IP格式: 1:22:33:4444,如果符合输出IP的4个部分内容cout << ">>>>>>>>>>>>>>testMatchResult" << endl;testMatchResult();// 4) 查找显示所有符合星期格式的单词:Saturdaycout << ">>>>>>>>>>>>>>testMultiSearch" << endl;testMultiSearch();// 5) 替换符合格式的文本内容:元音开头前面的a改成ancout << ">>>>>>>>>>>>>>regexReplace" << endl;regexReplace();// 6) 年月日格式的转换,将DD-MM-YYYY –> YYYY-MM-DDcout << ">>>>>>>>>>>>>>regexReplaceDate" << endl;regexReplaceDate();// 7) "new" and "delete" 出现的次数是否一样?cout << ">>>>>>>>>>>>>>searchCount" << endl;searchCount();return 0;
}

C++11正则表达式例子集合相关推荐

  1. c++11正则表达式(一)——匹配模式(pattern)

    一.作用 pattern 是一个c++字符串,它作用是给出一匹配规则,去检测给定字符串是否具备这种拼接规则. 二.模式字符串的语法规则 需要插入一句题外话,在特殊元字符中会用到\,对于c/c++而言它 ...

  2. Django基础(11): 表单集合Formset的高级用法详解

    Formset(表单集)是多个表单的集合.Formset在Web开发中应用很普遍,它可以让用户在同一个页面上提交多张表单,一键添加多个数据,比如一个页面上添加多个用户信息.今天小编我就介绍下Djang ...

  3. python非贪婪、多行匹配正则表达式例子[转载]

    python非贪婪.多行匹配正则表达式例子[转载] 一些regular的tips: 1 非贪婪flag >>> re.findall(r"a(\d+?)", &q ...

  4. python正则表达式处理txt_Python文本处理服务(re正则表达式例子)

    正则表达式例子 检查对子 在此示例中,我们将使用以下帮助函数来更优雅地显示匹配对象: def displaymatch(match):    if match is None:        retu ...

  5. python语言创意绘画是什么-Python街机模块的draw系列绘画例子集合

    """ draw系列绘画例子集合 """ import arcade import os # 设置工作目录,以python -m启动程序时才 ...

  6. python语言创意绘画-Python街机模块的draw系列绘画例子集合

    """ draw系列绘画例子集合 """ import arcade import os # 设置工作目录,以python -m启动程序时才 ...

  7. linux学习11 正则表达式与文件格式化处理

    目录 1.正则表达式简介 2.grep的一些高级选项及参数 3.基础正则表达式练习 查找特定字符串 利用[ ]查找集合字符 行首行尾字符^$ 任意一个字符与重复字符 . * 限定RE字符范围{ } 基 ...

  8. Python疯狂讲义(常见模块:sys/os/random/time/json/正则表达式/set集合)

    九.常见模块 1.sys sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息 import sys list_sys = [] for e in dir(sys):if n ...

  9. SE API第10/11天:集合 ——>ArrayListals、Iterator遍历迭代器、增强for、List集→subList、集合间转换asList、排序sort | Map相关

    一.Java 集合框架 0.介绍: (0)什么是集合 -集合与数组一样,可以保存一组元素,并且提供了操作元素的相关方法,使用更方便. (1)集合框架被设计成要满足以下几个目标: 该框架必须是高性能的. ...

最新文章

  1. dask 使用_在Google Cloud上使用Dask进行可扩展的机器学习
  2. eslint 无法格式化ts_vscode保存代码,自动按照eslint规范格式化代码设置
  3. winform 分页控件分享(二)
  4. QML笔记-使用Row的时候要注意的地方(一定要指明高度和宽度)
  5. [BZOJ4539][HNOI2016]树(主席树)
  6. 小米解锁过程中验证失败怎么办,有什么处理方式
  7. 优质计算机教案,信息技术优秀教案
  8. linux系统可以安装企业微信,Linux 上安装微信客户端
  9. django 分拆views 提示 module ‘app001.views‘ has no attribute ‘xxx‘
  10. html代码在线运行_在线运行html代码
  11. 游戏开发之NGUI字体集制作
  12. 全栈python之路——三篇文章带你踏入python大门-基础01
  13. cada0图纸框_CAD的图框应该怎么画-百度经验
  14. 第三次作业(蒋鑫和贺俊朋组)
  15. 图像的二值化原理和实现
  16. net start mysql启动mysql时报错:发生系统错误 2。找不到指定文件
  17. 冒泡排序详解及代码(Java)
  18. 罗斯蒙特RBI-K-546-R-DT-C-S-05-15-E-T传感器
  19. 关于Win10/11系统上安装Ubuntu双系统的rufus方法及实践问题
  20. 【华人学者风采】唐金辉 南京理工大学

热门文章

  1. 为什么人们默认 x86 代表 32 位处理器
  2. MAX3222/MAX3232/ MAX3237/MAX3241/串口通信中文_技术文档
  3. 【科普】Web(瓦片)地图的工作原理
  4. 界面按钮太多 聊天机器人都快被玩坏了
  5. 笨办法学C 练习28:Makefile 进阶
  6. DOM与元素节点内联样式
  7. 在EO中获取某字段基于表的列名
  8. iOS_11_tableViewCell使用alertView变更数据
  9. 史上最全PHP正则表达式实例汇总
  10. sql 存储过程中top 后面跟参数的问题