zoukankan      html  css  js  c++  java
  • C++ regex库的三种正则表达式操作

    关于正则表达式的语法和字符含义,网上已经有很不错的博客教学,我当初参考的是

    读懂正则表达式就这么简单 - Zery - 博客http://www.cnblogs.com/zery/p/3438845.html

    正则表达式 – 语法 | 菜鸟教程  (http://www.runoob.com/regexp/regexp-syntax.html)

    我在这里重点说明如何使用C++的regex库完成正则匹配,正则查找,正则替换三种操作

    1. 首先是头文件
      1 #include<regex>
      2 using namespace std;
    2. 正则表达式声明
      string str("\d{4}");
      regex pattern(str,regex::icase);

      注意与一般应用正则表达式不同,这里的转义符号要用“\”

    3. 匹配结果存放变量声明
      1     //第一种存储方式
      2     match_results<string::const_iterator> result;
      3     //第二种存储方式
      4     smatch result;

      这两个类都可以存储匹配得到的结果,建议使用第二种,比较方便

    4. 数据准备
      1     //文本数据
      2     string str="1994 is my birth year";
    5. 正则操作
      • 正则匹配
        1     //正则匹配
        2     string regex_str2("(\d{4}).*");
        3     regex pattern2(regex_str2,regex::icase);
        4 
        5     if(regex_match(str,result,pattern2)){
        6         cout<<result[0]<<endl;
        7         cout<<result[1]<<endl;
        8     }

        注意正则匹配的运算规则是先检查正则表达式是否与文本数据一致,只有在一致的条件下才会将匹配结果送入result中。例如将正则表达式改为("\d{4}"),返回值为FALSE,result中根本没有结果。下图是运行结果。我们从中看出result[0]是完整的文本,result[1]是第一个分组匹配的数据。如果正则表达式有n个分组,result的size也就是n+1个

           

      • 正则查找
         1     //文本数据
         2     string str="1994 is my birth year";
         3     //正则表达式
         4     string regex_str("\d{4}");
         5     regex pattern1(regex_str,regex::icase);
         6 
         7     //迭代器声明
         8     string::const_iterator iter = str.begin();
         9     string::const_iterator iterEnd= str.end();
        10     string temp;
        11     //正则查找
        12     while (std::regex_search(iter,iterEnd,result,pattern1))
        13     {
        14         temp=result[0];
        15         cout<<temp<<endl;
        16         iter = result[0].second; //更新搜索起始位置
        17     }

        首先声明迭代器,在用while循环查找,每一次查找只会匹配一个结果

      • 正则替换
        //正则替换
            std::regex reg1("\d{4}");
            string t("1993");
            str = regex_replace(str,reg1,t); //trim_left
            cout<<str<<endl;

        在str查找匹配的文本,并用t中的数据替换。经检验,这个函数会遍历整个文本变量,也就是文本变量中所有符合正则表达式的数据都会被替换

    以上就是我的经验总结,希望能帮到你。

     最后附上所有代码

     1 int main(){
     2 
     3     //第一种存储方式
     4     //match_results<string::const_iterator> result;
     5     //第二种存储方式
     6     smatch result;
     7 
     8     //文本数据
     9     string str="1994 is my birth year 1994";
    10     //正则表达式
    11     string regex_str("\d{4}");
    12     regex pattern1(regex_str,regex::icase);
    13 
    14     //迭代器声明
    15     string::const_iterator iter = str.begin();
    16     string::const_iterator iterEnd= str.end();
    17     string temp;
    18     //正则查找
    19     while (std::regex_search(iter,iterEnd,result,pattern1))
    20     {
    21         temp=result[0];
    22         cout<<temp<<endl;
    23         iter = result[0].second; //更新搜索起始位置
    24     }
    25 
    26     //正则匹配
    27     string regex_str2("(\d{4}).*");
    28     regex pattern2(regex_str2,regex::icase);
    29 
    30     if(regex_match(str,result,pattern2)){
    31         cout<<result[0]<<endl;
    32         cout<<result[1]<<endl;
    33     }
    34     
    35     //正则替换
    36     std::regex reg1("\d{4}");
    37     string t("1993");
    38     str = regex_replace(str,reg1,t); //trim_left
    39     cout<<str<<endl;
    40         
    41     return 0;
    42 }
  • 相关阅读:
    TestNG:org.openqa.selenium.firefox.NotConnectedException: Unable to connect
    Python 程序员经常犯的 10 个错误
    python爬虫框架scrapy实例详解
    使用python进行汉语分词
    Python监控日志程序
    用Python读取excel中的数据
    Python 文件处理
    10 款最好的 Python IDE
    自动化测试代码架构浅谈
    Robotium如何向下拖动屏幕
  • 原文地址:https://www.cnblogs.com/sgdd123/p/7778599.html
Copyright © 2011-2022 走看看