一个合法的取件码的格式为 A-B-C。其中 A为一位或两位数字B为一位数字C为四位数字且不能有多余的数字或符号包括换行符。现在大宁有一段短信他太懒了以至于不想看它请你为他提取出所有合法的取件码并 按出现顺序 输出c++
#include
int main() { std::string message; // 输入的短信内容 std::getline(std::cin, message);
std::regex pattern("\\b\\d{1,2}-\\d-\\d{4}\\b"); // 正则表达式匹配合法的取件码
std::smatch matches;
std::vector<std::string> codes;
// 在短信中查找所有匹配的取件码
std::string::const_iterator searchStart(message.cbegin());
while (std::regex_search(searchStart, message.cend(), matches, pattern)) {
codes.push_back(matches[0]);
searchStart = matches.suffix().first;
}
// 输出所有合法的取件码
for (const std::string& code : codes) {
std::cout << code << std::endl;
}
return 0;
原文地址: https://www.cveoy.top/t/topic/hYHL 著作权归作者所有。请勿转载和采集!