①头文件包含 #include
②明确声明正则表达式
std::regex reg("(((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))");
更多正则表达式可查阅:https://blog.csdn.net/Next_Second/article/details/126696589
③CString 转string
CString strIP = _T("127.0.0.1");
CT2CA IpTem(strIP);
std::string Ip(IpTem);
④进行匹配
std::regex_match(Ip, reg)//匹配上返回true,否则返回false
完整函数
BOOL CheckIPValid( CString strIP )
{
std::regex reg("(((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))");
CT2CA IpTem(strIP);
std::string Ip(IpTem);
if (std::regex_match(Ip, reg))
{
return TRUE;
}
else
{
return FALSE;
}
}