PCRE函数简介和使用示例
PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能。但它同时也实现了DFA,只是满足数学意义上的正则。 PCRE提供了19个接口函数,为了简单介绍,使用PCRE内带的测试程序(pcretest.c)示例用法。 1. pcre_compile 原型: #include <pcre.h> pcre *pcre_compile(const char *pattern,int options,const char **errptr,int *erroffset,const unsigned char *tableptr); 功能:将一个正则表达式编译成一个内部表示,在匹配多个字符串时,可以加速匹配。其同pcre_compile2功能一样只是缺少一个参数errorcodeptr。 参数: pattern正则表达式 options为0,或者其他参数选项 errptr出错消息 erroffset出错位置 tableptr指向一个字符数组的指针,可以设置为空NULL 示例: L1720re = pcre_compile((char *)p,options,&error,&erroroffset,tables); 2. pcre_compile2 pcre *pcre_compile2(const char *pattern,int *errorcodeptr,51)">功能:将一个正则表达式编译成一个内部表示,在匹配多个字符串时,可以加速匹配。其同pcre_compile功能一样只是多一个参数errorcodeptr。 errorcodeptr存放出错码 3. pcre_config int pcre_config(int what,void *where); 功能:查询当前PCRE版本中使用的选项信息。 what选项名 where存储结果的位置 Line1312(void)pcre_config(PCRE_CONFIG_POSIX_MALLOC_THRESHOLD,&rc);
4. pcre_copy_named_substring int pcre_copy_named_substring(const pcre *code,const char *subject,int *ovector,int stringcount,const char *stringname,char *buffer,int buffersize); 功能:根据名字获取捕获的字串。 code成功匹配的模式 subject匹配的串 ovectorpcre_exec()使用的偏移向量 stringcountpcre_exec()的返回值 stringname捕获字串的名字 buffer用来存储的缓冲区 buffersize缓冲区大小 示例: Line2730int rc = pcre_copy_named_substring(re,(char *)bptr,use_offsets, count,(char *)copynamesptr,copybuffer,sizeof(copybuffer)); 5.pcre_copy_substring int pcre_copy_substring(const char *subject,int stringnumber,51)">功能:根据编号获取捕获的字串。 stringnumber捕获字串编号 Line2730 int rc = pcre_copy_substring((char *)bptr,count, i,sizeof(copybuffer)); 6.pcre_dfa_exec int pcre_dfa_exec(const pcre *code,const pcre_extra *extra,int length,int startoffset,int ovecsize,int *workspace,int wscount); 功能:使用编译好的模式进行匹配,采用的是一种非传统的方法DFA,只是对匹配串扫描一次(与Perl不兼容)。 code编译好的模式 extra指向一个pcre_extra结构体,可以为NULL subject需要匹配的字符串 length匹配的字符串长度(Byte) startoffset匹配的开始位置 options选项位 ovector指向一个结果的整型数组 ovecsize数组大小 workspace一个工作区数组 wscount数组大小 Line2730count = pcre_dfa_exec(re,extra,len,start_offset, options | g_notempty,use_size_offsets,workspace,51)">sizeof(workspace)/sizeof(int)); 7.pcre_copy_substring int pcre_exec(const pcre *code,int ovecsize); 功能:使用编译好的模式进行匹配,采用与Perl相似的算法,返回匹配串的偏移位置。。 8.pcre_free_substring void pcre_free_substring(const char *stringptr); 功能:释放pcre_get_substring()和pcre_get_named_substring()申请的内存空间。 stringptr指向字符串的指针 Line2730const char *substring; int rc = pcre_get_substring((char *)bptr,&substring); …… pcre_free_substring(substring); 9.pcre_free_substring_list void pcre_free_substring_list(const char **stringptr); 功能:释放由pcre_get_substring_list申请的内存空间。 stringptr指向字符串数组的指针 Line2773const char **stringlist; int rc = pcre_get_substring_list((char *)bptr,51)">…… pcre_free_substring_list(stringlist); 10.pcre_fullinfo int pcre_fullinfo(const pcre *code,int what,51)">功能:返回编译出来的模式的信息。 extrapcre_study()的返回值,或者NULL what什么信息 where存储位置 Line997if ((rc = pcre_fullinfo(re,study,option,ptr)) < 0) fprintf(outfile,"Error %d from pcre_fullinfo(%d)/n",rc,option); } 11.pcre_get_named_substring int pcre_get_named_substring(const pcre *code,const char **stringptr); stringptr存放结果的字符串指针 Line2759const char *substring; int rc = pcre_get_named_substring(re,51)">count,(char *)getnamesptr,&substring); 12.pcre_get_stringnumber int pcre_get_stringnumber(const pcre *code,const char *name); 功能:根据命名捕获的名字获取对应的编号。 name捕获名字 13.pcre_get_substring int pcre_get_substring(const char *subject,51)">功能:获取匹配的子串。 subject成功匹配的串 stringnumber获取的字符串编号 stringptr字符串指针 14.pcre_get_substring_list int pcre_get_substring_list(const char *subject,const char ***listptr); 功能:获取匹配的所有子串。 listptr字符串列表的指针 15.pcre_info int pcre_info(const pcre *code,int *optptr,int *firstcharptr); 已过时,使用pcre_fullinfo替代。 16.pcre_maketables const unsigned char *pcre_maketables(void); 功能:生成一个字符表,表中每一个元素的值不大于256,可以用它传给pcre_compile()替换掉内建的字符表。 Line2759tables = pcre_maketables(); 17.pcre_refcount int pcre_refcount(pcre *code,int adjust); 功能:编译模式的引用计数。 code已编译的模式 adjust调整的引用计数值 18.pcre_study pcre_extra *pcre_study(const pcre *code,const char **errptr); 功能:对编译的模式进行学习,提取可以加速匹配过程的信息。 options选项 Line1797extra = pcre_study(re,study_options,&error); 19.pcre_version char *pcre_version(void); 功能:返回PCRE的版本信息。 Line1384if (!quiet) fprintf(outfile,"PCRE version %s/n/n",pcre_version()); 程序实例:
[cpp]view plaincopy
程序来自网上,看到有人不理解最后一个for循环的含义,ovector返回的是匹配字符串的偏移,包括起始偏移和结束偏移,所以就有循环内部的2*i处理。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |