/*
* 输入是789.123.456, 输出的是789
*/
void get()
{
std::regex ip_reg("(.*)\.123\.456");
std::smatch matchResult;
string inputStr;
std::getline(std::cin,inputStr);
//正则匹配
if (std::regex_match(inputStr,matchResult,ip_reg))
{
cout << "Match: ";
//打印子表达式结果
for (size_t i = 1; i < matchResult.size(); ++i)
{
cout << matchResult[i] << " ";
}
}
else
{
cout << "Not Match!";
}
}
C++正则表达式提取匹配到的字符串
最新推荐文章于 2026-05-02 05:28:53 发布
本文介绍了一个使用C++标准库中的正则表达式功能从字符串中提取特定模式的例子。通过一个具体的输入输出案例,展示了如何匹配并提取字符串前缀。

4208

被折叠的 条评论
为什么被折叠?



