pcre函数详解
转载地址:https://blog.csdn.net/wxq1987525/article/details/7574017
PCRE提供了19个接口函数:
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 正则表达式option 为0,或者其他参数选项errptr 出错信息erroffset 出错信息 tableptr 指向一个字符数组的指针,可以设置为空NULL 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 为0,或者其他参数选项errorcodeptr 存放出错码errptr 出错消息erroffset 出错位置tableptr 指向一个字符数组的指针,可以设置为空NULL 3.pcre_config原型:#include <pcre.h>int pcre_config(int what, void *where);功能:查询当前PCRE版本中使用的选项信息。参数:what 选项名where 存储结果的位置 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 pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值stringname 捕获字串的名字buffer 用来存储的缓冲区buffersize 缓冲区大小 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 pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值stringnumber 捕获字串编号buffer 用来存储的缓冲区buffersize 缓冲区大小 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 指向一个pcre_extra结构体,可以为NULLsubject 需要匹配的字符串length 匹配的字符串长度(Byte)startoffset 匹配的开始位置options 选项位ovector 指向一个结果的整形数组ovecsize 数组大小workspace 一个工作区数组wscount 数组大小 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 指向一个pcre_extra结构体,可以为NULLsubject 需要匹配的字符串length 匹配的字符串长度(Byte)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 指向字符串的指针 9.pcre_free_substring_list原型:#include <pcre.h>void pcre_free_substring_list(const char **stringptr);功能:释放由pcre_get_substring_list申请的内存空间参数:stringptr 指向字符串数组的指针 10.pcre_fullinfo原型:#include <pcre.h>int pcre_fullinfo(const pcre *code, const pcre_extra *extra, int what, void *where);功能:返回编译出来的模式的信息参数:code 编译好的模式extra pcre_study()的返回值,或者NULLwhat 什么信息where 存储位置 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 pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值stringname 捕获字串的名字stringptr 存放结果的字符串指针 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 pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值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 pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值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()替换掉它。 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 出错消息 19.pcre_version原型:#include <pcre.h>char *pcre_version(void);功能:返回PCRE的版本信息总结
- 上一篇: 随想之一人一句
- 下一篇: 记录ishield遇到的问题的解决过程