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();
}
原文地址:https://www.cnblogs.com/my-love-is-python/p/15059626.html