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

PCRE提供了19个接口函数,为了简单介绍,使用PCRE内带的测试程序(pcretest.c)示例用法。 

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

. 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 

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

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

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

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

. 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   数组大小 

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

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

fprintf(outfile, "Error %d from pcre_fullinfo(%d)/n", rc, option); 

} 

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

. pcre_get_stringnumber 

       原型: 

#include <pcre.h> 

int pcre_get_stringnumber(const pcre *code, const char *name); 

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

参数: 

code                            成功匹配的模式 

name                 捕获名字 

. 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      字符串指针 

. 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             字符串列表的指针 

. pcre_info 

       原型: 

#include <pcre.h> 

int pcre_info(const pcre *code, int *optptr, int *firstcharptr); 

已过时,使用pcre_fullinfo替代。 

. pcre_maketables 

       原型: 

#include <pcre.h> 

const unsigned char *pcre_maketables(void); 

功能:生成一个字符表,表中每一个元素的值不大于256,可以用它传给pcre_compile()替换掉内建的字符表。 

参数: 

示例: 

Line2759 tables = pcre_maketables(); 

. pcre_refcount 

       原型: 

#include <pcre.h> 

int pcre_refcount(pcre *code, int adjust); 

功能:编译模式的引用计数。 

参数: 

code       已编译的模式 

adjust      调整的引用计数值 

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

. pcre_version 

       原型: 

#include <pcre.h> 

char *pcre_version(void); 

功能:返回PCRE的版本信息。 

参数: 

示例: 

Line1384 if (!quiet) fprintf(outfile, "PCRE version %s/n/n", pcre_version()); 

pcre函数详解的更多相关文章

  1. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  2. NSSearchPathForDirectoriesInDomains函数详解

    NSSearchPathForDirectoriesInDomains函数详解     #import "NSString+FilePath.h" @implementation ...

  3. JavaScript正则表达式详解(二)JavaScript中正则表达式函数详解

    二.JavaScript中正则表达式函数详解(exec, test, match, replace, search, split) 1.使用正则表达式的方法去匹配查找字符串 1.1. exec方法详解 ...

  4. Linux C popen()函数详解

    表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...

  5. kzalloc 函数详解(转载)

    用kzalloc申请内存的时候, 效果等同于先是用 kmalloc() 申请空间 , 然后用 memset() 来初始化 ,所有申请的元素都被初始化为 0. view plain /** * kzal ...

  6. Netsuite Formula > Oracle函数列表速查(PL/SQL单行函数和组函数详解).txt

    PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句,函数主要分为两大类: 单行函数 ...

  7. jQuery.attr() 函数详解

    一,jQuery.attr()  函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...

  8. memset函数详解

    语言中memset函数详解(2011-11-16 21:11:02)转载▼标签: 杂谈 分类: 工具相关  功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大 ...

  9. CreateFile函数详解

    CreateFile函数详解 CreateFile The CreateFile function creates or opens the following objects and returns ...

随机推荐

  1. 4.6 基于STM32+MC20地图显示路径功能

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  2. 剑指offer 面试18题

    面试18题: 题目:删除链表中的节点 题一:在O(1)时间内删除链表节点.给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. 解题思路:我们要删除节点i,先把i的下一个节点j ...

  3. [转]linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例

    原文链接:http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html linux shell有一套自己的流程控制语句,其中包括条件语句 ...

  4. MVC4 中使用 Area 和 注意的地方

    在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰. 步骤如下:  项目 –> 添加 -> 区域 (Area)  输入 Admin 添加成功后 Area包含:创建一个空 ...

  5. Python函数之—— 装饰器(Day13)

    一.什么是装饰器 顾名思义,装饰器指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 二.装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数 ...

  6. 前端基础之JavaScript_(5)_DOM对象总结

    DOM 定义了访问和操作HTML文档的标准方法 访问(查找标签) //---- 直接查找 document.getElementById(“idname”) document.getElementsB ...

  7. 每天一个Linux命令(50)netstat命令

        netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况.     (1)用法:     用法:  netstat [选项参数]     (2)功能: ...

  8. mysql安装配置 (单个mysql安装)

    mysql安装于 c盘因为有注册文件 和注册表 1.为了防止mysql数据表变大和sql日志增加   改变C:\ProgramData\MySQL\MySQL Server 5.7\my.ini 得指 ...

  9. 【TECH】CAS php客户端配置

    搞完java又搞php,我整个人都不好了=.= 跟大师在linux上折腾了一下午,没调出来,早上在windows上跑通了,中午终于在linux上搞定了,嘿嘿. server端配置参见这里 在windo ...

  10. 交叉编译Mesa,X11lib,Qt opengl

    记录Mesa配置文件如下: Mesa版本:Mesa-10.2.3 CC=/usr/local/arm-4.8.1/bin/arm-none-linux-gnueabi-gcc CXX=/usr/loc ...