正则表达式校验IP地址

#include <vcl.h>
#include <pcre.h>

bool isIPValid(UnicodeString ipStr)

{
char ipAddrPattern[]="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
    const char      *pszErr;
    int             nErrOffset;
pcre *re = pcre_compile(ipAddrPattern,0,&pszErr,&nErrOffset,NULL);
    if(re==NULL)
    {
return false;
}
const int vectorSize = 30;
int ovector[vectorSize];    // 数量由szReg决定,大致为(括号对数+1)*3,可以用pcre_info获得或者直接留大一点。
int rc = pcre_exec(re, NULL, ipStr.c_str(), ipStr.Length(), 0, ovector, vectorSize);   // 执行匹配
free(re);


if(rc>=0)
return true;
else
     return false;
}
原文地址:https://www.cnblogs.com/jerry1999/p/3677346.html