• C++基础-正则表达式 regex_match(匹配) regex_search(查找) regex_replace(替换)


    正则匹配中的基础符号

    ^开头
    ()组
    []或,
    {}几次
    $结尾

    1.regex_match(匹配) 判断当前的结构体是否符合正则匹配规则

    #include<iostream>
    #include<regex>
    
    using namespace std;
    
    //regex_match 匹配
    //regex_search 查找
    //regex_replace 替换
    
    
    int main1()
    {
        regex reg("([a-zA-Z]*) ([a-zA-Z]*)$");
        cmatch what; //匹配的词语检索出来
        bool isit = regex_match("id admin", what, reg); //
        for(int i = 0; i !=what.size(); ++i) //输出匹配信息
        {
            cout << what[i+1].first << "	";
        }
        cout << "match" << endl;
        if(isit)
        {
    
        }else
        {
            cout << "not match" << endl;
        }
        cin.get();
    }

    2.regex_search 判断数字是否在目标结构体中

    int main3()
    {
        cmatch match;
        regex reg("\d+"); //数字
        char str[50] = ("hello 8848 hello huahua180");
        bool isOk = regex_search(str, match, reg);
        std::cout << isOk << endl;
        if(isOk)
        {
            for(int i = 0; i != match.size(); i++)
            {
                cout << match[i].first << endl;
            }
        }
        cin.get();
    }

    3.regex_replace(替换)

    将符合匹配条件的数字替换成其他的类型

    int main()
    {
    
        cmatch match;
        regex reg("\d+"); //数字
        char str[50] = ("hello 8848, hello huahua180");
        cout << regex_replace(str, reg, "ABCDEFG") << endl;
        cin.get();
    }
  • 相关阅读:
    L255 Learning to say no brings a thrill of freedom
    2019-02-25 EST 科技文翻译
    L253 Work and Pleasure
    2019.2.22 初级英语口语班 结课
    L252 小组作业
    2019-02-22 L231
    leetcode 67 Add Binary ----- java
    leetcode 66 Plus One ----- java
    leetcode 65 Valid Number ----- java
    leetcode 64 Minimum Path Sum ----- java
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/15059626.html
Copyright © 2020-2023  润新知