cocos2dx使用正则式例子

本文提供了一个跨平台的IP地址验证函数,使用正则表达式来检查输入的字符串是否符合IPv4地址的标准格式。针对不同的操作系统(如Windows和其他平台),该函数采用不同的实现方式。
加入头文件:

#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

}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值