歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> PCRE函數詳解

PCRE函數詳解

日期:2017/3/1 9:46:19   编辑:Linux編程

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

示例:

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);

功能:將一個正則表達式編譯成一個內部表示,在匹配多個字符串時,可以加速匹配。其同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 存儲結果的位置

示例:

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 pcre_exec() 使用的偏移向量

stringcount pcre_exec()的返回值

stringname 捕獲字串的名字

buffer 用來存儲的緩沖區

buffersize 緩沖區大小

示例:

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);

功能:根據編號獲取捕獲的字串。

參數:

code 成功匹配的模式

subject 匹配的串

ovector pcre_exec() 使用的偏移向量

stringcount pcre_exec()的返回值

stringnumber 捕獲字串編號

buffer 用來存儲的緩沖區

buffersize 緩沖區大小

示例:

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);

功能:使用編譯好的模式進行匹配,采用的是一種非傳統的方法DFA,只是對匹配串掃描一次(與Perl不兼容)。

參數:

code 編譯好的模式

extra 指向一個pcre_extra結構體,可以為NULL

subject 需要匹配的字符串

length 匹配的字符串長度(Byte)

startoffset 匹配的開始位置

options 選項位

ovector 指向一個結果的整型數組

ovecsize 數組大小

workspace 一個工作區數組

wscount 數組大小

示例:

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);

功能:使用編譯好的模式進行匹配,采用與Perl相似的算法,返回匹配串的偏移位置。。

參數:

code 編譯好的模式

extra 指向一個pcre_extra結構體,可以為NULL

subject 需要匹配的字符串

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 指向字符串的指針

示例:

Line2730 const char *substring;

int rc = pcre_get_substring((char *)bptr, use_offsets, count,

i, &substring);

……

pcre_free_substring(substring);


PCRE 的詳細介紹:請點這裡
PCRE 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved