企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 正则表达式 regex regex表示有一个正则表达式类regex\_match将一个字符序列与一个正则表达式匹配 regex\_search寻找第一个与正则表达式匹配的子序列 regex\_replace使用给定格式替换一个正则表达式 sregex\_iterator迭代适配器, 调用regex\_search来遍历一个string中所有匹配的子串smatch容器类, 保存在string中搜索的结果ssub\_matchstring中匹配的子表达式的结果 ~~~ #include <iostream> #include <string> #include <regex> int main(void) { std::string str1[] = {"foo.txt", "b.txt", "abcd.txt", "txt", "a0.txt", "AAA.txt", "wt.txt"}; std::regex txtRegex("[a-z]+\\.txt"); std::regex baseRegex("([a-z]+)\\.txt"); std::smatch baseMatch; for (const auto &s : str1) { if (std::regex_match(s, baseMatch, baseRegex)) { std::cout << "baseMatch[0]: " << baseMatch[0] << std::endl; std::cout << "baseMatch.szie: " << baseMatch.size() << std::endl; if (baseMatch.size() == 2) { std::string str = baseMatch[0].str(); std::string base = baseMatch[1].str(); std::cout << "str: " << str << std::endl; std::cout << s << " : " << base << std::endl; } } } return 0; } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)