时间 : 20-09-27 栏目 : linux编程 作者 : 老薛 评论 : 0 点击 : 208 次
在研究zabbix的 监控项类型时,看到snmptrap方式,在了解功能实现过程中,顺便找了个有关正则的实例程序跑了一下,做个笔记。
#define PCRE_STATIC // 静态库编译选项 #include <stdio.h> #include <string.h> #include <pcre.h> #define OVECCOUNT 30 /* should be a multiple of 3 */ #define EBUFLEN 128 #define BUFLEN 1024 int main() { pcre *re; const char *error; int erroffset; int ovector[OVECCOUNT]; int rc, i; char src [] = "555 <title>I am OK</title> 666"; char pattern [] = "<title>(.*)</(tit)le>"; printf("String : %s\n", src); printf("Pattern: \"%s\"\n", pattern); re = pcre_compile(pattern, 0, &error, &erroffset, NULL); if (re == NULL) { printf("PCRE compilation failed at offset %d: %s\n", erroffset, error); return 1; } rc = pcre_exec(re, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT); if (rc < 0) { if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n"); else printf("Matching error %d\n", rc); pcre_free(re); return 1; } printf("OK, has matched ...\n"); for (i = 0; i < rc; i++) { char *substring_start = src + ovector[2*i]; int substring_length = ovector[2*i+1] - ovector[2*i]; printf("$%2d: %.*s\n", i, substring_length, substring_start); } pcre_free(re); return 0; }
本文标签: pcre_exec函数实例
除非注明,文章均为( 老薛 )原创,转载请保留链接: http://www.bdkyr.com/xtyw003/2978.html