PCRE函数详解

<

div id=”content” contentScore=”5790″>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 的详细介绍:<a title=”PCRE” href=”../../Linux