pcre函数详解
轉(zhuǎn)載地址:https://blog.csdn.net/wxq1987525/article/details/7574017
PCRE提供了19個(gè)接口函數(shù):
1.pcre_compile原型:#include <pcre.h>pcre *pcre_compile(const char *pattern, int options, const char **errptr, int *erroffset, const unsigned char *tableptr);功能:講一個(gè)正則表達(dá)式編譯成一個(gè)內(nèi)部表示,在匹配多個(gè)字符串時(shí),可以加速匹配。其同pcre_compile2功能一樣只是缺少一個(gè)參數(shù)errorcodeptr。參數(shù):pattern 正則表達(dá)式option 為0,或者其他參數(shù)選項(xiàng)errptr 出錯(cuò)信息erroffset 出錯(cuò)信息 tableptr 指向一個(gè)字符數(shù)組的指針,可以設(shè)置為空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);功能:將一個(gè)正則表達(dá)式編譯成一個(gè)內(nèi)部表示,在匹配多個(gè)字符串時(shí),可以加速匹配。其同pcre_compile功能一樣只是多一個(gè)參數(shù)errorcodeptr。參數(shù):pattern 正則表達(dá)式options 為0,或者其他參數(shù)選項(xiàng)errorcodeptr 存放出錯(cuò)碼errptr 出錯(cuò)消息erroffset 出錯(cuò)位置tableptr 指向一個(gè)字符數(shù)組的指針,可以設(shè)置為空NULL 3.pcre_config原型:#include <pcre.h>int pcre_config(int what, void *where);功能:查詢當(dāng)前PCRE版本中使用的選項(xiàng)信息。參數(shù):what 選項(xiàng)名where 存儲(chǔ)結(jié)果的位置 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);功能:根據(jù)名字獲取捕獲的字串參數(shù):code 成功匹配的模式subject 匹配的串ovector pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值stringname 捕獲字串的名字buffer 用來存儲(chǔ)的緩沖區(qū)buffersize 緩沖區(qū)大小 5.pcre_copy_substring原型:#include <pcre.h>int pcre_copy_substring(const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int buffersize);功能:根據(jù)編號(hào)獲取捕獲的字串參數(shù):code 成功匹配的模式subject 匹配的串ovector pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值stringnumber 捕獲字串編號(hào)buffer 用來存儲(chǔ)的緩沖區(qū)buffersize 緩沖區(qū)大小 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);功能:使用編譯好的模式進(jìn)行匹配,采用的是一種非傳統(tǒng)的方法DFA,只是對(duì)匹配串掃描一次(與Perl不兼容)參數(shù):code 編譯好的模式extra 指向一個(gè)pcre_extra結(jié)構(gòu)體,可以為NULLsubject 需要匹配的字符串length 匹配的字符串長(zhǎng)度(Byte)startoffset 匹配的開始位置options 選項(xiàng)位ovector 指向一個(gè)結(jié)果的整形數(shù)組ovecsize 數(shù)組大小workspace 一個(gè)工作區(qū)數(shù)組wscount 數(shù)組大小 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);功能:使用編譯好的模式進(jìn)行匹配,采用與Perl相似的算法,返回匹配串的偏移位置參數(shù):code 編譯好的模式extra 指向一個(gè)pcre_extra結(jié)構(gòu)體,可以為NULLsubject 需要匹配的字符串length 匹配的字符串長(zhǎng)度(Byte)startoffset 匹配的開始位置options 選項(xiàng)位ovector 指向一個(gè)結(jié)果的整形數(shù)組ovecsize 數(shù)組大小 8.pcre_free_substring原型:#include <pcre.h>void pcre_free_substring(const char *stringptr);功能:釋放pcre_get_substring()和pcre_get_named_substring()申請(qǐng)的內(nèi)存空間參數(shù):stringptr 指向字符串的指針 9.pcre_free_substring_list原型:#include <pcre.h>void pcre_free_substring_list(const char **stringptr);功能:釋放由pcre_get_substring_list申請(qǐng)的內(nèi)存空間參數(shù):stringptr 指向字符串?dāng)?shù)組的指針 10.pcre_fullinfo原型:#include <pcre.h>int pcre_fullinfo(const pcre *code, const pcre_extra *extra, int what, void *where);功能:返回編譯出來的模式的信息參數(shù):code 編譯好的模式extra pcre_study()的返回值,或者NULLwhat 什么信息where 存儲(chǔ)位置 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);功能:根據(jù)編號(hào)獲取捕獲的字串參數(shù):code 成功匹配的模式subject 匹配的串ovector pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值stringname 捕獲字串的名字stringptr 存放結(jié)果的字符串指針 12.pcre_get_stringnumber原型:#include <pcre.h>int pcre_get_stringnumber(const pcre *code, const char *name);功能:根據(jù)命名捕獲的名字獲取對(duì)應(yīng)的編號(hào)參數(shù):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);功能:獲取匹配的子串參數(shù):subject 成功匹配的串ovector pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值stringnumber 獲取的字符串編號(hào)stringptr 字符串指針 14.pcre_get_substring_list原型:#include <pcre.h>int pcre_get_substring_list(const char *subject, int *ovector, int stringcount, const char ***listptr);功能:獲取匹配的所有子串參數(shù):subject 成功匹配的串ovector pcre_exec()使用的偏移向量stringcount pcre_exec()的返回值listptr 字符串列表的指針 15.pcre_info原型:#include <pcre.h>int pcre_info(const pcre *code, int *optptr, int *firstcharptr);//已過時(shí),使用pcre_fullinfo替代 16.pcre_maketables原型:#include <pcre.h>const unsigned char *pcre_maketables(void);功能:生成一個(gè)字符表,表中每一個(gè)元素的值不大于256,可以用它傳給pcre_compile()替換掉它。 17.pcre_refcount原型:#include <pcre.h>int pcre_refcount(pcre *code, int adjust);功能:編譯模式的引用計(jì)數(shù)參數(shù):code 已編譯的模式adjust 調(diào)整的引用計(jì)數(shù)值 18.pcre_study原型:#include <pcre.h>pcre_extra *pcre_study(const pcre *code, int options, const char **errptr);功能:對(duì)編譯的模式進(jìn)行學(xué)習(xí),提取可以加速匹配過程的信息。參數(shù):code 已編譯的模式options 選項(xiàng)errptr 出錯(cuò)消息 19.pcre_version原型:#include <pcre.h>char *pcre_version(void);功能:返回PCRE的版本信息總結(jié)
- 上一篇: 随想之一人一句
- 下一篇: Nginx开发一个简单的HTTP过滤模块