http://blog.sina.com.cn/s/blog_6a1837e901010ckv.html 2012 一、PCRE的安装: 1、主页地址:http://www./ 2、解压缩: 3、配置: 4、编译: 5、安装: 6、检查: 7、将库文件导入cache: 8、使用: 也可用apt直接安装: apt-cache search pcre 查找pcre 下面只安装pcrecpp apt-get install libpcre++-dev apt-get install libpcre++0 二、函数简介和使用示例: PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能。但它同时也实现了DFA,只是满足数学意义上的正则。 PCRE提供了19个接口函数,为了简单介绍,使用PCRE内带的测试程序(pcretest.c)示例用法。 1. pcre_compile pcre *pcre_compile(const char *pattern, int options, const char **errptr, int *erroffset, const unsigned char *tableptr); 功能:将一个正则表达式编译成一个内部表示,在匹配多个字符串时,可以加速匹配。其同pcre_compile2功能一样只是缺少一个参数errorcodeptr。 参数: pattern options tableptr 示例: L1720 2. pcre_compile2 #include <pcre.h> pcre *pcre_compile2(const char *pattern, int options, int *errorcodeptr, const char **errptr, int *erroffset, const unsigned char *tableptr); 功能:将一个正则表达式编译成一个内部表示,在匹配多个字符串时,可以加速匹配。其同pcre_compile功能一样只是多一个参数errorcodeptr。 参数: pattern options errorcodeptr tableptr 3. pcre_config #include <pcre.h> 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 #include <pcre.h> int pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector, int stringcount, const char *stringname, char *buffer, int buffersize); 功能:根据名字获取捕获的字串。 参数: code subject ovector stringcount stringname buffer buffersize 示例: Line2730 int rc = pcre_copy_named_substring(re, (char *)bptr, use_offsets, 5. pcre_copy_substring #include <pcre.h> int pcre_copy_substring(const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int buffersize); 功能:根据编号获取捕获的字串。 参数: code subject ovector stringcount stringnumber buffer buffersize 示例: Line2730 int rc = pcre_copy_substring((char *)bptr, use_offsets, count, 6. pcre_dfa_exec #include <pcre.h> int pcre_dfa_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize, int *workspace, int wscount); 功能:使用编译好的模式进行匹配,采用的是一种非传统的方法DFA,只是对匹配串扫描一次(与Perl不兼容)。 参数: code extra subject length startoffset options ovector ovecsize workspace wscount 示例: Line2730 count = pcre_dfa_exec(re, extra, (char *)bptr, len, start_offset, 7. pcre_copy_substring #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize); 功能:使用编译好的模式进行匹配,采用与Perl相似的算法,返回匹配串的偏移位置。。 参数: code extra subject length startoffset options ovector ovecsize 8. pcre_free_substring #include <pcre.h> void pcre_free_substring(const char *stringptr); 功能:释放pcre_get_substring()和pcre_get_named_substring()申请的内存空间。 参数: stringptr 示例: Line2730 int rc = pcre_get_substring((char *)bptr, use_offsets, count, …… pcre_free_substring(substring); 9. pcre_free_substring_list #include <pcre.h> void pcre_free_substring_list(const char **stringptr); 功能:释放由pcre_get_substring_list申请的内存空间。 参数: stringptr 示例: Line2773 int rc = pcre_get_substring_list((char *)bptr, use_offsets, count, …… pcre_free_substring_list(stringlist); 10. pcre_fullinfo #include <pcre.h> int pcre_fullinfo(const pcre *code, const pcre_extra *extra, int what, void *where); 功能:返回编译出来的模式的信息。 参数: code extra what where 示例: Line997 fprintf(outfile, "Error %d from pcre_fullinfo(%d)/n", rc, option); } 11. pcre_get_named_substring #include <pcre.h> int pcre_get_named_substring(const pcre *code, const char *subject, int *ovector, int stringcount, const char *stringname, const char **stringptr); 功能:根据编号获取捕获的字串。 参数: code subject ovector stringcount stringname stringptr 示例: Line2759 int rc = pcre_get_named_substring(re, (char *)bptr, use_offsets, 12. pcre_get_stringnumber #include <pcre.h> int pcre_get_stringnumber(const pcre *code, const char *name); 功能:根据命名捕获的名字获取对应的编号。 参数: code name 13. pcre_get_substring #include <pcre.h> int pcre_get_substring(const char *subject, int *ovector, int stringcount, int stringnumber, const char **stringptr); 功能:获取匹配的子串。 参数: subject ovector stringcount stringnumber stringptr 14. pcre_get_substring_list #include <pcre.h> int pcre_get_substring_list(const char *subject, int *ovector, int stringcount, const char ***listptr); 功能:获取匹配的所有子串。 参数: subject ovector stringcount listptr 15. pcre_info #include <pcre.h> int pcre_info(const pcre *code, int *optptr, int *firstcharptr); 已过时,使用pcre_fullinfo替代。 16. pcre_maketables #include <pcre.h> const unsigned char *pcre_maketables(void); 功能:生成一个字符表,表中每一个元素的值不大于256,可以用它传给pcre_compile()替换掉内建的字符表。 参数: 示例: Line2759 tables = pcre_maketables(); 17. pcre_refcount #include <pcre.h> int pcre_refcount(pcre *code, int adjust); 功能:编译模式的引用计数。 参数: code adjust 18. pcre_study #include <pcre.h> pcre_extra *pcre_study(const pcre *code, int options, const char **errptr); 功能:对编译的模式进行学习,提取可以加速匹配过程的信息。 参数: code options errptr 示例: Line1797 extra = pcre_study(re, study_options, &error); 19. pcre_version #include <pcre.h> char *pcre_version(void); 功能:返回PCRE的版本信息。 参数: 示例: Line1384 if (!quiet) fprintf(outfile, "PCRE version %s/n/n", pcre_version()); 示例代码: #define PCRE_STATIC
//
静态库编译选项
备:ovector返回的是匹配字符串的偏移,包括起始偏移和结束偏移,所以就有循环内部的2*i处理。 三、pcre++ CODE:
CODE:
|
|