判断IPV6还是IPV4的接口最容易想到的就是使用正则表达式,但是C/C++对正则表达式的支持并不能满足要求。所以提供了一个对于正则表达式的读写匹配一些函数。
以下是对一些匹配正则表达式的函数接口的介绍,并不全,但是应该对下面的接口使用够用。
C process the regular expression
OSI C/C++ don't suppose the regular expression
1、使用正则表达式的步骤
Step1:compile the regcom()
Step2: match the reg regexec()
Step3: release the reg regfree()
Step4: get the reg regcomp or regexec()
2、declaration the functions
int regcomp(regex_t *compiled, const char* pattern, int cflags)
1)regex_t data struct ,saved the compile pattern reg expresssion
re_nsub saved the child expression
pattern -- saved the reg expression
cflags -- REG_EXITENDED -- 已功能更加强大的扩展正则表达式的方式进行匹配
REG_ICASE -- 匹配子母时忽略大小写
REG_NOSUB -- 不用存储匹配后的结果
REG_NEWLINE -- 识别换行符
int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags)
①compiled 是已经用regcomp函数编译好的正则表达式。
②string 是目标文本串。
③nmatch 是regmatch_t结构体数组的长度。
④matchptr regmatch_t类型的结构体数组,存放匹配文本串的位置信息。
⑤eflags 有两个值
void regfree (regex_t *compiled)
4. size_t regerror (int errcode, regex_t *compiled, char *buffer, size_t length)
当执行regcomp 或者regexec 产生错误的时候,就可以调用这个函数而返回一个包含错误信息的字符串。
参数说明:
①errcode 是由regcomp 和 regexec 函数返回的错误代号。
②compiled 是已经用regcomp函数编译好的正则表达式,这个值可以为NULL。
③buffer 指向用来存放错误信息的字符串的内存空间。
④length 指明buffer的长度,如果这个错误信息的长度大于这个值,则regerror 函数会自动截断超出的字符串,但他仍然会返回完整的字符串的长度。所以我们可以用如下的 方法先得到错误字符串的长度。
size_t length = regerror (errcode, compiled, NULL, 0);
以上的函数可以在linux下man查看更为详细的信息
下面是IPV4和IPV6的正则表达式的匹配规则
/* Pattern Reg Define */
/* IPv4 pattern */
#define PATTERN_IPV4 "^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\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\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|" \
"([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){0,4}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])" \
"(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4})" \
"{0,5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}))$"
以下是通用的函数接口,可以参照函数接口的使用说明,就可以达到想要的使用效果
/********************************************************************************
*Func Name: Match_RegExpPattern
*Func Function: match the reg expression
*Func Params : const char* inputpattern: input the reg expression
char *inputbuf : the regexpression for match
int matchmode : intput the compile the reg expression mode
have four mode : one or more mode can be using one time
REG_EXTENDED: Extended Regular Expression syntax when interpreting regex
REG_ICASE: ingore the low or big zimu
REG_NOSUB: ingore the match pattern
REG_NEWLINE: Noingore the \n
*Result from Func: int : true -- match successfully; fales -- match failed
*Author : cdd
*Data of Creation: 2018-02-03
*Data of Modification: none
*Author of Modification: none
*Reason for Modification:none
*Version : 1.0
*History of Version : 1.0
**********************************************************************************/
int Match_RegExpPattern(const char* inputpattern, char *inputbuf, int matchmode)
{
int iRet = 0;
int mStatus = 0;
int mMode = matchmode;
const size_t nMatch = 1;
regmatch_t pMatch[1] = {'\0'};
regex_t cRegPattern;
if(NULL == inputpattern || NULL == inputbuf)
{
return false;
}
/* Compile the input pattern */
iRet = regcomp(&cRegPattern, inputpattern, mMode);
if(iRet)
{
return false;
}
/* Exec the patterns */
mStatus = regexec(&cRegPattern, inputbuf, nMatch, pMatch, 0);
if(REG_NOMATCH == mStatus)
{
regfree(&cRegPattern);
return false;
}
else if(0 == mStatus)
{
regfree(&cRegPattern);
return true;
}
regfree(&cRegPattern);
return false;
}