码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • c++primer 正则表达式详细介绍和实例


    正则表达式(regular expression)_好大一只鸡的博客-CSDN博客

    正则表达式(Regular Expression)基本概念及理解_X1nZz的博客-CSDN博客_正则表达式的定义

    ECMAScript正则表达式_HuiDT的博客-CSDN博客_ecmascript正则表达式

    详解C++正则表达式 - 红叶空间 - 博客园
    C/C++ 正则表达式 regex库介绍(详细版)_余识-的博客-CSDN博客_regex库

    https://blog.csdn.net/digitalkee/article/details/128763847?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22128763847%22%2C%22source%22%3A%22digitalkee%22%7D 

    正则表达式又称为: 规则表达式。
    作用:一般用于 检索 或 者 替换某些符合所选设置规则(或模式)的文本
    使用:正则表达式是普通字符或者一些特殊字符组成的字符模式,将此作为一个字符模板进行与所需要的文本进行字符串的模糊匹配,以达到所需要的效果。

    正则表达式常用字符:

    在这里插入图片描述

    在这里插入图片描述

     头文件: #include 

    三种用法:
    regex_match :全文匹配,即要求整个字符串符合匹配规则,返回true或false(不改变字符串本身)
    regex_search:搜索匹配,即搜索字符串中存在符合规则的子字符串。
    regex_replace: 替换匹配,即可以将符合匹配规则的子字符串替换为其他字符串。(会改变字符串本身)

    1. include <regex>
    2. #include <iostream>
    3. #include <string>
    4. using namespace std;
    5. int main1()
    6. {
    7. string n;
    8. int res = 0;
    9. while (!res)
    10. {
    11. cout << "请输入手机号" << endl;
    12. cin >> n;
    13. res = regex_match(n, regex("132\\d{8}"));
    14. cout << res << endl;
    15. if (res == 0)
    16. {
    17. cout << "输入格式不正确" << endl;
    18. }
    19. }
    20. return 0;
    21. }
    1. //\d表示一个数字:但是在具体的使用中,必须要写成\\d,因为\\表示一个杠
    2. //3个数字用\d{3}表示,具体匹配中,写作\\d{3}
    3. //单个\表示转移字符
    4. #include <string>
    5. #include <iostream>
    6. #include <regex>
    7. using namespace std;
    8. int main()
    9. {
    10. // \\d{3}表示3个数字
    11. string pattern("\\d{3}\\d{8}");
    12. regex r(pattern);
    13. smatch results;
    14. string str;
    15. cin >> str;
    16. if(regex_match(str,results,r))
    17. {
    18. cout << "success match:";
    19. cout << results.str() << endl;
    20. }
    21. return 0;
    22. }

    有问题的一段代码:下面的代码,匹配 三个数字,0个或者1个-、.、空格,但是如果输入

    123 12312312就不能完成匹配。为何?

    1. #include <string>
    2. #include <iostream>
    3. #include <regex>
    4. using namespace std;
    5. int main()
    6. {
    7. try{
    8. string pattern("\\d{3}([-. ])?\\d{8}");
    9. regex r(pattern,regex::icase);
    10. smatch results;
    11. string file;
    12. cin >> file;
    13. if(regex_match(file,results,r))
    14. {
    15. cout << "yes,matched:";
    16. cout << results.str() << endl;
    17. }
    18. }
    19. catch (regex_error e)
    20. {
    21. cout << e.what() << endl;
    22. cout << e.code() << endl;
    23. }
    24. return 0;
    25. }
    1. #include <string>
    2. #include <iostream>
    3. #include <regex>
    4. #include <vector>
    5. using namespace std;
    6. int main()
    7. {
    8. string pattern("(\\d{3})([ .-])?(\\d{4})");
    9. regex r(pattern);
    10. smatch results;
    11. vector<string> v{"123 1234","123.1234","123-1234","1231234"};
    12. for(auto &i : v)
    13. {
    14. if(regex_match(i,results,r))
    15. {
    16. cout << "matched:" << results.str() << endl;
    17. }
    18. }
    19. return 0;
    20. }

    运行结果:

    1. matched:123 1234
    2. matched:123.1234
    3. matched:123-1234
    4. matched:1231234

    正则表达式的另一个例子:

    1. #include <string>
    2. #include <iostream>
    3. #include <regex>
    4. #include <vector>
    5. using namespace std;
    6. int main()
    7. {
    8. string pattern = "\\(?\\d{3}\\)?[ -.]?\\d{4}";
    9. regex r(pattern);
    10. smatch results;
    11. vector<string> str = {"(123 1234","123.1234","123-1234","1231234","123123123","123 1234)","(123).1234","123).1234","(123)1234"};
    12. for(auto &word : str)
    13. { if(regex_match(word,results,r))
    14. {
    15. cout << results.str() << endl;
    16. }
    17. }
    18. return 0;
    19. }
    1. (123 1234
    2. 123.1234
    3. 123-1234
    4. 1231234
    5. (123).1234
    6. 123).1234
    7. (123)1234

    im.cc 是c++primer 655页里面的一个例子:

    1. #include <string>
    2. #include <iostream>
    3. #include <regex>
    4. #include <vector>
    5. using namespace std;
    6. int main()
    7. {
    8. string pattern = "(\\()?(\\d{3})(\\))?([ .-])?(\\d{4})([ .-]?)(\\d{4})";
    9. regex r(pattern,regex::icase);
    10. smatch results;
    11. string line;
    12. while(getline(cin,line))
    13. {
    14. for(sregex_iterator it(line.begin(),line.end(),r),end; it != end; ++ it)
    15. {
    16. cout << "matched:" << endl;
    17. cout << it -> str() << endl;
    18. }
    19. }
    20. return 0;
    21. }
    22. ~

    运行结果:

    1. r@r:~/coml/c++/c++_primer/17/17.3/17.3.3/issue_code$ ./123
    2. 12312341234 (123)1234-1234 (123.12341234 (123)-1234-1234
    3. matched:
    4. 12312341234
    5. matched:
    6. (123)1234-1234
    7. matched:
    8. (123.12341234
    9. matched:
    10. (123)-1234-1234

  • 相关阅读:
    施努卡:什么是视觉定位系统 视觉定位系统的工作原理
    华钜同创:跨境电商运营如何优化Listing文案
    23种设计模式之责任链模式
    R语言——朴素贝叶斯文本分类
    【leetcode】【剑指offer Ⅱ】064. 神奇的字典
    将轨迹显示到卫星地图上
    专访 | 许伟 ——贡献榜 Top4 也只是“开源小白”
    NISP二级考试有什么要求吗?
    【Node.js】模块化:
    scipy Matlab-style IIR 滤波器设计下(Elliptic \Bessel \IIR-notch\IIR-peak\IIR-comb)
  • 原文地址:https://blog.csdn.net/digitalkee/article/details/127685912
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号