返回顶部

收藏

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

»更多 您可能感兴趣的代码
  1. 2014-06-19 13:49:14C++ URL 编码/解码 by sxgkwei
  2. 2014-08-24 13:03:50Linux下简单的病毒程序(C) by 蟋蟀哥
  3. 2014-05-19 18:23:46用c语言生成六位的随机密码。 by 童学芬
  4. 2014-07-21 12:47:08指向结构体变量的指针的简单应用 by niutao.linux
  5. 2014-07-26 20:12:47生成7对11求模校验和 by lucasli
  6. 2014-10-12 20:50:54通过openssl进行证书签名的一段代码 by Kevin.
  7. 2014-05-22 18:08:51提升权限来自MSDN by 小项
  8. 2014-05-28 20:00:31随机生成密码 by lucasli
  9. 2014-10-21 12:13:49一个简单的C语言加密程序 by walker30
  10. 2014-06-20 21:45:52ElGamal密码算法 by 小项
  11. 2014-06-21 10:05:45n皇后 by 跳跳虎