• regex正则表达式库学习


    在程序开发时,我们有时遇需要过滤一些特殊字符,或者匹配一些固定的字符等,比如电话号码都是数字,电子邮箱包含@符号,密码不能包含特殊字符等,这些都是可以用正则表达式来实现,很多高级语言都提供这个库比如python,java等C++11之前没有这个库所以处理这些很麻烦,从c++11开始就提供了正则表达式的库在头文件regex中。

    函数

    regex_match

    (C++11)

    尝试匹配一个正则表达式到整个字符序列
    (函数模板)

    regex_search

    (C++11)

    尝试匹配一个正则表达式到字符序列的任何部分
    (函数模板)

    regex_replace

    (C++11)

    以格式化的替换文本来替换正则表达式匹配的出现位置
    (函数模板)

    下面我们看它的一些简单示例。

    1.regex_match的使用

    1. #include
    2. #include
    3. using namespace std;
    4. //regex_match
    5. int main()
    6. {
    7. //1.尝试匹配一个正则表达式到整个字符序列,若匹配存在则返回true,否则返回false.
    8. //1.1 简单正则表达式匹配
    9. std::string fnames[] = {"yak.txt", "scott.txt", "yak.txt", "run.dat", "caleml"};
    10. std::regex txt_regex("[a-z]+\\.txt");
    11. for(const auto &fname: fnames)
    12. {
    13. cout << fname << ": " << regex_match(fname, txt_regex) << endl;
    14. }
    15. cout << endl;
    16. //1.2提取子匹配
    17. regex base_regex("([a-z]+)\\.txt");
    18. smatch base_match;
    19. for(const auto &fname: fnames)
    20. {
    21. if(regex_match(fname, base_match, base_regex))
    22. {
    23. cout << fname << " base_match.size() " << base_match.size() << endl;
    24. //首个sub_match是整个字符串,下个sub_match是首个括号表达式。
    25. if(base_match.size() == 2)
    26. {
    27. ssub_match base_sub_math = base_match[1];
    28. string base = base_sub_math.str();
    29. cout << fname << " has a base of " << base << endl;
    30. }
    31. }
    32. }
    33. cout << endl;
    34. //3. 提取几个子匹配
    35. std::regex pieces_regex("([a-z]+)\\.([a-z]+)");
    36. std::smatch pieces_match;
    37. for (const auto &fname : fnames)
    38. {
    39. if (std::regex_match(fname, pieces_match, pieces_regex))
    40. {
    41. std::cout << fname << endl;
    42. for (size_t i = 0; i < pieces_match.size(); ++i)
    43. {
    44. std::ssub_match sub_match = pieces_match[i];
    45. std::string piece = sub_match.str();
    46. std::cout << " submatch " << i << ": " << piece << '\n';
    47. }
    48. }
    49. }
    50. cout << "Hello World!" << endl;
    51. return 0;
    52. }

    运行结果:

     2.regex_search

    1. #include
    2. #include
    3. using namespace std;
    4. //regex_search
    5. int main()
    6. {
    7. string target("baaaby");
    8. smatch sm;
    9. regex re1("a(a)*b");
    10. regex_search(target, sm, re1);
    11. cout << "entire match: " << sm.str(0) << '\n'
    12. << "submatch #1: " << sm.str(1) << '\n';
    13. regex re2("a(a*)b");
    14. regex_search(target, sm, re2);
    15. cout << "entire match: " << sm.str(0) << '\n'
    16. << "submatch #1: " << sm.str(1) << '\n';
    17. cout << "hello world!" << endl;
    18. return 0;
    19. }

    运行结果:

    3.regex_replace

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. string text = "Quick brown fox";
    7. regex vowel_re("a|e|i|o|u");
    8. // 写结果到输出迭代器
    9. regex_replace(std::ostreambuf_iterator<char>(std::cout),
    10. text.begin(), text.end(), vowel_re, "*");
    11. // 构造保有结果的字符串
    12. cout << '\n' << std::regex_replace(text, vowel_re, "[$&]") << endl;
    13. cout << "hello world!" << endl;
    14. return 0;
    15. }

    运行结果:

    参考:

    - C++ Reference

    标准库头文件 - cppreference.com

  • 相关阅读:
    Python 字符串详解
    《Deep Convolution Neural Networks for Twitter Sentiment Analysis》文献研读
    QT中QString 转换为 char *的几种方法
    初识javaweb2 tomcat
    取消win10/win11下的路径长度限制
    45部署LVS-DR群集
    akshare复权算法-港股复权后数据代码分享
    java8概要
    Python线性代数傅里叶分析和动态系统模拟分析之一
    统一身份认证实现,推广的可能性及优缺点?
  • 原文地址:https://blog.csdn.net/chenyijun/article/details/125822311