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

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

ovector              pcre_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       成功匹配的串

ovector       pcre_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       成功匹配的串

ovector       pcre_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());

20.pcre_exec

  原型:

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

功能:用来检查某个字符串是否与指定的正则表达式匹配

参数: code, 输入参数,用pcre_compile编译好的正则表达结构的指针

     extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针

     subject, 输入参数,要被用来匹配的字符串

     length, 输入参数, 要被用来匹配的字符串的指针

     startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量

     options, 输入参数, 用来指定匹配过程中的一些选项

   ovector, 输出参数,用来返回匹配位置偏移量的数组

     ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小

返回值:匹配成功返回非负数,匹配返回负数

PCRE函数简介和使用示例的更多相关文章

  1. PCRE函数简介和使用示例【转】

    PCRE函数简介和使用示例 标签: 正则表达式listbuffercompilationnullperl 原文地址:http://blog.csdn.net/sulliy/article/detail ...

  2. Linux Shell系列教程之(十五) Shell函数简介

    本文是Linux Shell系列教程的第(十五)篇,更多Linux Shell教程请看:Linux Shell系列教程 函数可以将一个复杂功能划分成若干模块,从而使程序结构更加清晰,代码重复利用率更高 ...

  3. [转]SQLITE3 C语言接口 API 函数简介

    SQLITE3 C语言接口 API 函数简介 说明:本说明文档属作者从接触 SQLite 开始认识的 API 函数的使用方法, 由本人翻译, 不断更新. /* 2012-05-25 */ int sq ...

  4. Python3 hasattr()、getattr()、setattr()函数简介

    Python3 hasattr().getattr().setattr()函数简介 一.hasattr(object, name) 判断object对象中是否存在name属性,当然对于python的对 ...

  5. epoll 系列函数简介、与select、poll 的区别

    一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...

  6. fork() 函数简介

    fork() 函数简介 fork系统调用用于创建一个新进程,称为子进程,它与进行fork()调用的进程(父进程)并发运行.创建新的子进程后,两个进程都将执行fork()系统调用之后的下一条指令.子进程 ...

  7. Python Sklearn.metrics 简介及应用示例

    Python Sklearn.metrics 简介及应用示例 利用Python进行各种机器学习算法的实现时,经常会用到sklearn(scikit-learn)这个模块/库. 无论利用机器学习算法进行 ...

  8. linux进程编程:子进程创建及执行函数简介

    linux进程编程:子进程创建及执行函数简介 子进程创建及执行函数有三个: (1)fork();(2)exec();(3)system();    下面分别做详细介绍.(1)fork()    函数定 ...

  9. 【液晶模块系列基础视频】4.5.X-GUI图形界面库-进度条等函数简介

    [液晶模块系列基础视频]4.5.X-GUI图形界面库-进度条等函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地址 ...

随机推荐

  1. ubuntu服务器切换语言

    如果在安装Ubuntu Server时选择了中文,在系统安装完毕后,默认是中文,在操作时经常会显示乱码,如果需要设置回英文,则修改/etc/default/locale,将 LANG="cn ...

  2. COGS 1406. 邻居年龄排序[Age Sort,UVa 11462](水题日常)

    ★   输入文件:AgeSort.in   输出文件:AgeSort.out   简单对比时间限制:1 s   内存限制:2 MB [题目描述] Mr.Zero(CH)喜闻乐见地得到了一台内存大大增强 ...

  3. 自定义Jquery 下拉框

    (function ($){ 'use strict'; var g_id = 0; var g_open_id = []; $.fn.select3 = function () { var _id ...

  4. (转)用@Resource注解完成属性装配

    http://blog.csdn.net/yerenyuan_pku/article/details/52858878 前面我们讲过spring的依赖注入有两种方式: 使用构造器注入. 使用属性set ...

  5. JS将时间戳转换为刚刚、N分钟前、今天几点几分、昨天几点几分等表示法

    使用Javascript语言,将时间戳转换为类似新浪微博的时间的表示方法. 要求转换规则: 1分钟以内显示为:刚刚 1小时以内显示为:N分钟前 当天以内显示为:今天 N点N分(如:今天 22:33) ...

  6. How to Slove MB SD C4 Cannot Access OBD2 Port

    When using china clone mb sd connect compact 4 Multiplexer, it could not link to the car computer, M ...

  7. 实训day02 python

    一.数据类型 列表: 定义:在[]内,可以存放多个任意类型的值,并以逗号隔开: 一般用于存放学生的爱好,课堂的周期等. 定义一个学生列表,可存放多个学生 students = ['A','B','C' ...

  8. PHP07 函数

    学习要点 函数的定义 自定义函数 函数的工作原理和结构化编程 PHP变量范围 声明及应用各种形式的PHP函数 递归函数 使用自定义函数库 匿名函数和闭包 常用PHP系统函数 PHP7函数新特性 函数的 ...

  9. HDU4300 Clairewd’s message(拓展kmp)

    Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...

  10. MySQL连接使用及分类

    SQL连接 SQL 连接(JOIN)子句用于将数据库中两个或者两个以上表中的记录组合起来.连接通过共有值将不同表中的字段组合在一起. 考虑下面两个表,(a)CUSTOMERS 表:+----+---- ...