pcre函數(shù)詳細(xì)解析
PCRE是一個(gè)NFA正則引擎,不然不能提供完全與Perl一致的正則語(yǔ)法功能。但它同時(shí)也實(shí)現(xiàn)了DFA,只是滿足數(shù)學(xué)意義上的正則。
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á)式
options 為0,或者其他參數(shù)選項(xiàng)
errptr出錯(cuò)消息
erroffset 出錯(cuò)位置
tableptr 指向一個(gè)字符數(shù)組的指針,可以設(shè)置為空NULL
示例:
L1720 re = pcre_compile((char *)p, options, &error, &erroroffset, tables);
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é)果的位置
示例:
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);
功能:根據(jù)名字獲取捕獲的字串。
參數(shù):
code成功匹配的模式
subject 匹配的串
ovectorpcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
stringname捕獲字串的名字
buffer 用來(lái)存儲(chǔ)的緩沖區(qū)
buffersize 緩沖區(qū)大小
示例:
Line2730 int rc = pcre_copy_named_substring(re, (char *)bptr, use_offsets,
count, (char *)copynamesptr, copybuffer, sizeof(copybuffer));
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 匹配的串
ovectorpcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
stringnumber 捕獲字串編號(hào)
buffer 用來(lái)存儲(chǔ)的緩沖區(qū)
buffersize 緩沖區(qū)大小
示例:
Line2730 int rc = pcre_copy_substring((char *)bptr, use_offsets, count,
i, copybuffer, sizeof(copybuffer));
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)體,可以為NULL
subject 需要匹配的字符串
length匹配的字符串長(zhǎng)度(Byte)
startoffset 匹配的開始位置
options 選項(xiàng)位
ovector 指向一個(gè)結(jié)果的整型數(shù)組
ovecsize 數(shù)組大小
workspace 一個(gè)工作區(qū)數(shù)組
wscount 數(shù)組大小
示例:
Line2730 count = pcre_dfa_exec(re, extra, (char *)bptr, len, start_offset,
options | g_notempty, use_offsets, use_size_offsets, workspace,
sizeof(workspace)/sizeof(int));
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)體,可以為NULL
subject 需要匹配的字符串
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 指向字符串的指針
示例:
Line2730 const char *substring;
int rc = pcre_get_substring((char *)bptr, use_offsets, count,
i, &substring);
……
pcre_free_substring(substring);
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ù)組的指針
示例:
Line2773 const char **stringlist;
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);
功能:返回編譯出來(lái)的模式的信息。
參數(shù):
code 編譯好的模式
extra pcre_study()的返回值,或者NULL
what 什么信息
where存儲(chǔ)位置
示例:
Line997 if ((rc = pcre_fullinfo(re, study, option, ptr)) < 0)
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);
功能:根據(jù)編號(hào)獲取捕獲的字串。
參數(shù):
code成功匹配的模式
subject 匹配的串
ovectorpcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
stringname捕獲字串的名字
stringptr 存放結(jié)果的字符串指針
示例:
Line2759 const char *substring;
int rc = pcre_get_named_substring(re, (char *)bptr, use_offsets,
count, (char *)getnamesptr, &substring);
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成功匹配的串
ovectorpcre_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成功匹配的串
ovectorpcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
listptr 字符串列表的指針
15. pcre_info
原型:
#include <pcre.h>
int pcre_info(const pcre *code, int *optptr, int *firstcharptr);
已過(guò)時(shí),使用pcre_fullinfo替代。
16. pcre_maketables
原型:
#include <pcre.h>
const unsigned char *pcre_maketables(void);
功能:生成一個(gè)字符表,表中每一個(gè)元素的值不大于256,可以用它傳給pcre_compile()替換掉內(nèi)建的字符表。
參數(shù):
示例:
Line2759 tables = pcre_maketables();
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í),提取可以加速匹配過(guò)程的信息。
參數(shù):
code 已編譯的模式
options 選項(xiàng)
errptr 出錯(cuò)消息
示例:
Line1797 extra = pcre_study(re, study_options, &error);
19. pcre_version
原型:
#include <pcre.h>
char *pcre_version(void);
功能:返回PCRE的版本信息。
參數(shù):
示例:
Line1384 if (!quiet) fprintf(outfile, "PCRE version %s/n/n", pcre_version());
相關(guān)文章
C/C++?實(shí)現(xiàn)動(dòng)態(tài)資源文件釋放的方法
當(dāng)我們開發(fā)Windows應(yīng)用程序時(shí),通常會(huì)涉及到使用資源(Resource)的情況。資源可以包括圖標(biāo)、位圖、字符串等,它們以二進(jìn)制形式嵌入到可執(zhí)行文件中,這篇文章主要介紹了C/C++?實(shí)現(xiàn)動(dòng)態(tài)資源文件釋放,需要的朋友可以參考下2023-12-12VS Code如何編寫C/C++程序的實(shí)現(xiàn)步驟
本文主要介紹了VS Code如何編寫C/C++程序的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C++實(shí)現(xiàn)LeetCode(114.將二叉樹展開成鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(114.將二叉樹展開成鏈表),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07VScode配置C++運(yùn)行環(huán)境的完整步驟
這篇文章主要給大家介紹了關(guān)于VScode配置C++運(yùn)行環(huán)境的完整步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C++實(shí)現(xiàn)LeetCode(89.格雷碼)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(89.格雷碼),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C/C++實(shí)現(xiàn)發(fā)送與接收HTTP/S請(qǐng)求的示例代碼
HTTP(Hypertext Transfer Protocol)是一種用于傳輸超文本的協(xié)議,它是一種無(wú)狀態(tài)的、應(yīng)用層的協(xié)議,用于在計(jì)算機(jī)之間傳輸超文本文檔,通常在 Web 瀏覽器和 Web 服務(wù)器之間進(jìn)行數(shù)據(jù)通信,本文給大家介紹了C/C++發(fā)送與接收HTTP/S請(qǐng)求,需要的朋友可以參考下2023-11-11C語(yǔ)言文件操作函數(shù)freopen詳細(xì)解析
替換一個(gè)流,或者說(shuō)重新分配文件指針,實(shí)現(xiàn)重定向。如果stream流已經(jīng)打開,則先關(guān)閉該流。如果該流已經(jīng)定向,則freopen將會(huì)清除該定向。此函數(shù)一般用于將一個(gè)指定的文件打開一個(gè)預(yù)定義的流:標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出或者標(biāo)準(zhǔn)出錯(cuò)2013-10-10