首页 > 编程语言 > 详细

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

时间:2021-07-26 09:27:11      阅读:16      评论:0      收藏:0      [点我收藏+]

正则匹配中的基础符号

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

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 << "\t";
    }
    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();
}

 

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

原文:https://www.cnblogs.com/my-love-is-python/p/15059626.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!