返回顶部

收藏

ip地址正则表达式测试是否正确

更多
#include <sys/types.h>
#include <regex.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

 char *test;
 /*
 int regcomp(regex_t *preg, const char *regex, int cflags);

 int regexec(const regex_t *preg, const char *string, size_t nmatch,
            regmatch_t pmatch[], int eflags);

 size_t regerror(int errcode, const regex_t *preg, char *errbuf,
                size_t errbuf_size);

 void regfree(regex_t *preg);
 */
 int main()
 {
     //编译的正则表达式
     regex_t preg;
     //正则表达式字符串
 //    char *pattern = "([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})";
//  char *pattern = "((?:(?:25[0-5]|2[0-4]d|((1d{2})|([1-9]?d))).){3}(?:25[0-5]|2[0-4]d|((1d{2})|([1-9]?d))))";
   // char * pattern = "<a>(.+?)</a>";
  //    char *pattern = "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])(\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])){3}";
//  char *pattern = "((2[0-4]\\d|25[0-5]|1?\\d?\\d)\\.){3}(2[0-4]\\d|25[0-5]|1?\\d?\\d)";
//  char *pattern = "2[0-4]\\d|25[0-5]|1\\d?\\d?|[2-99]\\.(2[0-4]\\d|25[0-5]|0|1\\d?\\d?|[2-99]\\.){2}2[0-4]\\d|25[0-5]|0|1\\d?\\d?|[2-99]";
//  char *pattern = "((25[0-5]|2[0-4]\\d|((1\\d{2})|[1-9]?\\d))\\.){3}|(25[0-5]|2[0-4]\\d|((1\\d{2})|[1-9]?\\d))";
//  char *pattern = "((25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2}).){3})(25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2}))"; 
//  char *pattern = "((25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2})\\.)((25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2})\\.)((25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2})\\.)(25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2}))";
    char *pattern = "([0-9]{1,3}[.]){3}[0-9]{1,3}";

//  char *pattern = "((25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2}).)((25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2}).)((25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2}).)(25[0-5]|(2[0-4][0-9])|([0-1][0-9]{1,2}))";
     //标志使用正则表达式标准扩展并且不区分大小写REG_EXTENDED|REG_ICASE
     int cflags = REG_EXTENDED |REG_NEWLINE , regcomp_return = 0;
     //错误字符串缓冲
     char errbuf[128];
     //第一步:编译生成正则表达式
     regcomp_return = regcomp(&preg, pattern, cflags);
     //如果成功返回0
     if (regcomp_return != 0) {
         regerror(regcomp_return, &preg, errbuf, sizeof(errbuf));
         fprintf(stderr, "%s:pattern '%s'", errbuf, pattern);
     printf("\\n");
         exit(1);
     }

     //第二步:使用生成的正则表达式匹配
    //char *str = "<a>192.168.1.2</a>";
//  char *str = "192.168.1.22";
    char *str;
//  scanf("%s\\n",str);
    printf("write ip:");
    gets(str);
     /*
     typedef struct {
        regoff_t rm_so;
        regoff_t rm_eo;
     } regmatch_t;
     rm_so 该字符串开始匹配的位置
     rm_eo 该字符串在结尾匹配的位置
     */
     regmatch_t pm[1];
     //最大匹配数填充到pm数组中
     const size_t nmatch = 1; 
     int z;
     z = regexec(&preg, str, nmatch, pm, 0);
     //如果z不等于0,那么
     if (z != 0) {
         regerror(z, &preg, errbuf, sizeof(errbuf));
         fprintf(stderr, "%s:pattern '%s'", errbuf, pattern);
     printf("\\n");
         exit(1);
     }
//     z = regexec(&preg, str, nmatch, pm, 0);
     int i = 0;

     //这里只会匹配第一个192.168.1.1要进行多次匹配需要将第一次匹配的结果摘除,进行循环匹配
     //提示:使用strcpy函数来摘除已经匹配的字符串
//     char *test;
     test = (char *)malloc(pm[0].rm_eo - pm[0].rm_so);

     for (i = pm[0].rm_so; i < pm[0].rm_eo; i++){
         putchar(str[i]);
//   int len = pm[0].rm_eo - pm[0].rm_so;
//   memset(cmatch,'\\0',sizeof(cmatch));
//   memcpy(cmatch,str+pm[i].rm_so,len);
//   printf("%s\\n",cmatch);
     }

     printf("\\n");
     memset(test, '\\0', sizeof(test));
//     memcpy(test, str+pm[0].rm_so,pm[0].rm_eo-pm[0].rm_so);
     memcpy(test,str, pm[0].rm_eo - pm[0].rm_so);
     printf("memcpy:%s\\n",test);
     free(test);
     //释放正则表达式
     regfree(&preg);
     printf("get %s\\n",str);

     return 1;
 }
//该片段来自于http://outofmemory.cn

标签:c++,安全

收藏

0人收藏

支持

0

反对

0

发表评论