cocos2dx使用正则式例子

来源:互联网 发布:不怎么花钱的网络手游 编辑:程序博客网 时间:2024/06/12 00:31
加入头文件:

#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

}
0 0
原创粉丝点击