加入头文件:
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
#include <io.h>
#include <regex>
#else
#include <regex.h>
#endif
测试代码:
// 检测是不是 ip 地址
bool checkIsIPAddress(std::string str)
{
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
const std::regex pattern( "((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])" );
return std::regex_match(str, pattern);
#else
bool isCorrect= true ;
char ss[200] = {0};
sprintf(ss, "%s" , str.c_str());
regmatch_t pmatch[4];
regex_t match_regex;
regcomp(&match_regex, "((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])" ,
REG_EXTENDED);
if (regexec(&match_regex, ss, 4, pmatch, 0) != 0)
{
isCorrect= false ;
}
regfree(&match_regex);
return isCorrect;
#endif
}
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
#include <io.h>
#include <regex>
#else
#include <regex.h>
#endif
测试代码:
// 检测是不是 ip 地址
bool checkIsIPAddress(std::string str)
{
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
const std::regex pattern( "((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])" );
return std::regex_match(str, pattern);
#else
bool isCorrect= true ;
char ss[200] = {0};
sprintf(ss, "%s" , str.c_str());
regmatch_t pmatch[4];
regex_t match_regex;
regcomp(&match_regex, "((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])" ,
REG_EXTENDED);
if (regexec(&match_regex, ss, 4, pmatch, 0) != 0)
{
isCorrect= false ;
}
regfree(&match_regex);
return isCorrect;
#endif
}