ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 概述 ## 实例 #### regex_match ``` #include <iostream> #include <string> #include <regex> int main() { std::string fname = "foo.txt"; std::regex txt_regex("[a-z]+\\.txt"); //是否匹配 if (std::regex_match(fname, txt_regex)) { std::cout << "true"; }else { std::cout << "false"; } } ``` ### regex_match 获取匹配值 ``` #include <iostream> #include <string> #include <regex> int main() { std::string fname = "foo.txt"; std::regex txt_regex("([a-z]+)\.txt"); std::smatch match; //是否匹配 if (std::regex_match(fname,match, txt_regex)) { std::cout << "true"; }else { std::cout << "false"; } std::cout << "szie="<< match.size()<<"\n"; if (match.size()==2) { std::cout << "0=" << match[0].str() << "\n"; // 0=foo.txt std::cout <<"1=" <<match[1].str()<<"\n"; // 1=foo } } ```