🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[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 } } ```