今天看帖子,看到有个网友用了正则式并出问题了,问题是windows上能匹配成功,android上无法匹配。这个问题我之前遇到过并解决了,所以就把测试例子弄上来了,希望可以让人少走弯路。
加入头文件:
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
#include <io.h>
#include
#else
#include <regex.h>
#endif
测试代码:
//检测是不是ip地址
bool checkIsIPAddress(std::string str)
{
#if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
const std::regex pattern("((25|2|1||).){3}(25|2|1||)");
return std::regex_match(str, pattern);
#else
bool isCorrect=true;
char ss = {0};
sprintf(ss, “%s”, str.c_str());
regmatch_t pmatch;
regex_t match_regex;
regcomp(&match_regex,"((25|2|1||).){3}(25|2|1||)",
REG_EXTENDED);
if (regexec(&match_regex, ss, 4, pmatch, 0) != 0)
{
isCorrect=false;
}
regfree(&match_regex);
return isCorrect;
#endif
}
写的好,顶一个,收藏了。
