|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <regex>
#include <iostream>
#include <string>
bool IsIpV4Address(const std::string& strIp)
{
//验证IP地址的模式,这里"\."中的"\"是转义字符,表示这是一个.
const std::regex pattern("(\\d{1,3}){1}\.(\\d{1,3}){1}\.(\\d{1,3}){1}\.(\\d{1,3}){1}");
//匹配验证
return std::regex_match(strIp, pattern);
}
int main()
{
std::string strIp1 = "13.34.34.4";//192.168.1.1
std::string strIp2 = "192.168";
std::cout << strIp1 << " : " << (IsIpV4Address(strIp1) ? "valid" : "invalid") << std::endl;
std::cout << strIp2 << " : " << (IsIpV4Address(strIp2) ? "valid" : "invalid") << std::endl;
system("pause");
return 0;
}
"(\\d{1,3}){1}\.(\\d{1,3}){1}\.(\\d{1,3}){1}\.(\\d{1,3}){1}"
\\d是什么意思?
如果我只输入10.10为什么会匹配成功?不是最起码得有3个.吗? |
|