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

pcre函数具体解释的更多相关文章

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

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

  2. openssl之EVP系列之8---EVP_Digest系列函数具体解释

    openssl之EVP系列之8---EVP_Digest系列函数具体解释     ---依据openssl doc/crypto/EVP_DigestInit.pod翻译和自己的理解写成     (作 ...

  3. scanf函数具体解释与缓冲区

    1.基本信息 函数原型: int scanf( char *format, args, ...); 函数返回值: 读入并赋给args的数据个数.遇到文件结束返回EOF,出错返回0. 函数功能: sca ...

  4. 经常使用socket函数具体解释

    经常使用socket函数具体解释 关于socket函数,每一个的意义和基本功能都知道,但每次使用都会去百度,參数究竟是什么,返回值代表什么意义.就是说用的少,也记得不够精确. 每次都查半天.常常烦恼于 ...

  5. openssl之EVP系列之5---EVP_Encrypt系列函数具体解释(二)

    openssl之EVP系列之5---EVP_Encrypt系列函数详细解释(二)    ---依据openssl doc/crypto/EVP_EncryptInit.pod和doc/ssleay.t ...

  6. malloc函数具体解释

    一.原型:extern void *malloc(unsigned int num_bytes); 头文件:#include <malloc.h> 或 #include <alloc ...

  7. CreateProcess函数具体解释

    CreateProcess说明:WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程执行指定的可执行文件. 函数原型:BOOL CreateProcess(    ...

  8. Android中SensorManager.getRotationMatrix函数原理解释

    SensorManager是Android中的一个类,其有一个函数getRotationMatrix,可以计算出旋转矩阵,进而通过getOrientation求得设备的方向(航向角.俯仰角.横滚角). ...

  9. linux中fork()函数具体解释(原创!!实例解说)

     一.fork入门知识 一个进程,包含代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程差点儿全然同样的进程,也就是两个进程能够做全然同样的事,但假设初始參数或者传入的变量不 ...

随机推荐

  1. Built-in Functions学习

    abs(x) :返回一个数字的绝对值,可以是整形也可以是浮点型. all(iterable):返回True,如果所以迭代对象的元素为true,或者可迭代对象为空. any(iterable):如果可迭 ...

  2. [摘]ASP.Net标准控件(TextBox控件)

    TextBox控件 TextBox控件又称文本框控件,为用户提供输入文本的功能. 1.属性 TextBox控件的常用属性及说明如表1所示. 表1 TextBox控件常用属性及说明 属    性 说   ...

  3. JY的题目(水)

    JY的题目[问题背景]一天,JY觉得DZY智商太低下,决定和他离婚,除非DZY做出来她出的题目.DZY当然非常想和JY在一起,所以他只好又去请计算机大神WJC帮忙,WJC已经帮过他N多次忙了,不想再帮 ...

  4. Java实现串口通信的小样例

    用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME ...

  5. PLSQLDeveloper过期要注册表

    打开运行输入 regedit 打表注册表 删除 HKEY_CURRENT_USER\Software\Allround Automations HKEY_CURRENT_USER\Software\M ...

  6. Ghost Button制作教程及设计趋势分析

    概述:Ghost Button(虚拟按钮)是网页设计中一个非常实用的按钮样式,特别是图片背景中,有出色的效果.今天我们一起来研究Ghost Button的各种效果的制作方法,并对Ghost Butto ...

  7. English - consist of 和 compose of 的区别

    comprise,compose,consist,constitute,include 这一组动词都有"组成,包含"的意思. comprise v.包含,包括,由……组成(整体): ...

  8. Excel文件转换为html静态网页

    Excel文件转换为html静态网页:右键另存为:

  9. Java中的compareTo()函数用法

    public int compareTo(String anotherString) 按字典顺序比较两个字符串.该比较基于字符串中各个字符的 Unicode 值.将此 String 对象表示的字符序列 ...

  10. Win8安装ASP.net 4.5(转)

    (原文:http://blog.csdn.net/dingxu_ren/article/details/17607451) 今天在我的电脑上部署Web程序时发现页面打不开,网上搜了下是因为先安装的.n ...