pcre函数详细解析

所属分类: 软件编程 / C 语言 阅读数: 89
收藏 0 赞 0 分享

PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能。但它同时也实现了DFA,只是满足数学意义上的正则。

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 匹配的串
ovectorpcre_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 匹配的串
ovectorpcre_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);


9. pcre_free_substring_list

原型:
#include <pcre.h>
void pcre_free_substring_list(const char **stringptr);

功能:释放由pcre_get_substring_list申请的内存空间。

参数:
stringptr     指向字符串数组的指针

示例:

复制代码 代码如下:

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

功能:返回编译出来的模式的信息。

参数:
code   编译好的模式
extra  pcre_study()的返回值,或者NULL
what  什么信息
where存储位置

示例:

复制代码 代码如下:

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

功能:根据编号获取捕获的字串。

参数:
code成功匹配的模式
subject 匹配的串
ovectorpcre_exec() 使用的偏移向量
stringcount   pcre_exec()的返回值
stringname捕获字串的名字
stringptr     存放结果的字符串指针

示例:

复制代码 代码如下:

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

功能:根据命名捕获的名字获取对应的编号。

参数:
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成功匹配的串
ovectorpcre_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成功匹配的串
ovectorpcre_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()替换掉内建的字符表。

参数:

示例:
Line2759 tables = pcre_maketables();

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     出错消息

示例:
Line1797 extra = pcre_study(re, study_options, &error);

19. pcre_version

原型:
#include <pcre.h>
char *pcre_version(void);

功能:返回PCRE的版本信息。
参数:
示例:
Line1384 if (!quiet) fprintf(outfile, "PCRE version %s/n/n", pcre_version());

更多精彩内容其他人还在看

用标准c++实现string与各种类型之间的转换

这个类在头文件中定义, < sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本
收藏 0 赞 0 分享

C++如何通过ostringstream实现任意类型转string

再使用整型转string的时候感觉有点棘手,因为itoa不是标准C里面的,而且即便是有itoa,其他类型转string不是很方便。后来去网上找了一下,发现有一个好方法
收藏 0 赞 0 分享

C/C++指针小结

要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内存区
收藏 0 赞 0 分享

C++ 类的静态成员深入解析

在C++中类的静态成员变量和静态成员函数是个容易出错的地方,本文先通过几个例子来总结静态成员变量和成员函数使用规则,再给出一个实例来加深印象
收藏 0 赞 0 分享

C++类的静态成员初始化详细讲解

通常静态数据成员在类声明中声明,在包含类方法的文件中初始化.初始化时使用作用域操作符来指出静态成员所属的类.但如果静态成员是整型或是枚举型const,则可以在类声明中初始化
收藏 0 赞 0 分享

C++类静态成员与类静态成员函数详解

静态成员不可在类体内进行赋值,因为它是被所有该类的对象所共享的。你在一个对象里给它赋值,其他对象里的该成员也会发生变化。为了避免混乱,所以不可在类体内进行赋值
收藏 0 赞 0 分享

C++中的friend友元函数详细解析

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。友元函数的特点是能够访问类中的私有成员的非成员函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数一样
收藏 0 赞 0 分享

static全局变量与普通的全局变量的区别详细解析

以下是对static全局变量与普通的全局变量的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助
收藏 0 赞 0 分享

C++ explicit关键字的应用方法详细讲解

C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢?下面就让我们一起来看看这方面的知识吧
收藏 0 赞 0 分享

教你5分钟轻松搞定内存字节对齐

随便google一下,人家就可以跟你解释的,一大堆的道理,我们没怎么多时间,讨论为何要对齐.直入主题,怎么判断内存对齐规则,sizeof的结果怎么来的,请牢记以下3条原则
收藏 0 赞 0 分享
查看更多