c++11正则表达式
创始人
2025-05-31 05:08:23

#include

#include

using namespace std;

#if 0

int main(){

    string str;

    cin >> str;

    cout << regex_match(str, regex("\\d{4}-\\d{1,2}"));

    return 0;

}

int main(){

    string str ;

    cout << "请输入邮箱:" << endl;

    while (cin >> str){

        if(regex_match(str, regex("[a-zA-Z0-9]+@1(26|63)\\.com")))

        {

            break;

        }

        cout << "输入错误,请重新输入:" << endl;

    }

    cout << "输入成功" <

    return 0;

}

//单个匹配

int main(){

    string str="hello2023-03-20log";

    smatch macth; //搜索的结果

    regex pattern("(\\d{4})-(\\d{1,2})-(\\d{1,2})"); //搜索规则 ()表示把内容拿出来

    if(regex_search(str,macth, pattern))

    {

        cout<< "年:" << macth[1] << endl;

        cout<< "月:" <

        cout<< "日:" << macth[3] << endl;

    }

    return 0;

}

// 多个匹配

int main(){

    string str="2023-03-21,2023-03-20,2023-03-19";

    smatch macth; //搜索的结果

    regex pattern("(\\d{4})-(\\d{1,2})-(\\d{1,2})"); //搜索规则 ()表示把内容拿出来

    string::const_iterator citer = str.begin();

    while (regex_search(citer, str.cend(),macth, pattern)) //循环匹配

    {

      citer =macth[0].second;

      for (size_t i= 1;i < macth.size(); ++i)

      {

        cout << macth[i] << " ";

      }

      cout << endl;

    }

    return 0;

}

#endif

int  main()

{

    cout << regex_replace("2023-03-20",regex("-"),"/") <

    return 0;

}

相关内容

热门资讯

南财观察 第148期:豆包手机... 现在我们和手机的交互是“人找服务”:需要自己打开微信看地址,再切到地图叫车。而在豆包手机上,你只需要...
出租车司机遭乘客殴打!警方通报... 转自:法治日报12月14日,内蒙古自治区呼和浩特市赛罕区公安分局发布通报称,12月13日,赛罕区公安...
泽连斯基:要求乌克兰单方面撤军... 转自:北京日报客户端据乌克兰国际文传电讯社14日报道,乌克兰总统泽连斯基当天表示,美国要求乌军单方面...
香港火灾受影响居民,将获中医义... 转自:北京日报客户端香港特区政府医务卫生香港局今日(14日)公布,明日起将为大埔宏福苑火灾受影响居民...
晨丰科技:股东杭州宏沃拟减持不... 证券日报网讯 12月14日晚间,晨丰科技公告称,持股4.3134%的股东杭州宏沃计划自2026年1月...