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

PCRE函数简介和使用示例

标签: 正则表达式listbuffercompilationnullperl

原文地址:http://blog.csdn.net/sulliy/article/details/6247155

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

程序实例:

 1 #define PCRE_STATIC // 静态库编译选项
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <pcre.h>
 5 #define OVECCOUNT 30 /* should be a multiple of 3 */
 6 #define EBUFLEN 128
 7 #define BUFLEN 1024
 8
 9 int main()
10 {
11     pcre  *re;
12     const char *error;
13     int  erroffset;
14     int  ovector[OVECCOUNT];
15     int  rc, i;
16     char  src [] = "111 <title>Hello World</title> 222";   // 要被用来匹配的字符串
17     char  pattern [] = "<title>(.*)</(tit)le>";              // 将要被编译的字符串形式的正则表达式
18     printf("String : %s/n", src);
19     printf("Pattern: /"%s/"/n", pattern);
20     re = pcre_compile(pattern,       // pattern, 输入参数,将要被编译的字符串形式的正则表达式
21                       0,            // options, 输入参数,用来指定编译时的一些选项
22                       &error,       // errptr, 输出参数,用来输出错误信息
23                       &erroffset,   // erroffset, 输出参数,pattern中出错位置的偏移量
24                       NULL);        // tableptr, 输入参数,用来指定字符表,一般情况用NULL
25     // 返回值:被编译好的正则表达式的pcre内部表示结构
26     if (re == NULL) {                 //如果编译失败,返回错误信息
27         printf("PCRE compilation failed at offset %d: %s/n", erroffset, error);
28         return 1;
29     }
30     rc = pcre_exec(re,            // code, 输入参数,用pcre_compile编译好的正则表达结构的指针
31                    NULL,          // extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针
32                    src,           // subject, 输入参数,要被用来匹配的字符串
33                    strlen(src),  // length, 输入参数, 要被用来匹配的字符串的指针
34                    0,             // startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量
35                    0,             // options, 输入参数, 用来指定匹配过程中的一些选项
36                    ovector,       // ovector, 输出参数,用来返回匹配位置偏移量的数组
37                    OVECCOUNT);    // ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小
38     // 返回值:匹配成功返回非负数,没有匹配返回负数
39     if (rc < 0) {                     //如果没有匹配,返回错误信息
40         if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match .../n");
41         else printf("Matching error %d/n", rc);
42         pcre_free(re);
43         return 1;
44     }
45     printf("/nOK, has matched .../n/n");   //没有出错,已经匹配
46     for (i = 0; i < rc; i++) {             //分别取出捕获分组 $0整个正则公式 $1第一个()
47         char *substring_start = src + ovector[2*i];
48         int substring_length = ovector[2*i+1] - ovector[2*i];
49         printf("$%2d: %.*s/n", i, substring_length, substring_start);
50     }
51     pcre_free(re);                     // 编译正则表达式re 释放内存
52     return 0;
53 }  

程序来自网上,看到有人不理解最后一个for循环的含义,ovector返回的是匹配字符串的偏移,包括起始偏移和结束偏移,所以就有循环内部的2*i处理。

时间: 2024-08-26 16:28:02

PCRE函数简介和使用示例【转】的相关文章

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

SQLITE3 C语言接口 API 函数简介 说明:本说明文档属作者从接触 SQLite 开始认识的 API 函数的使用方法, 由本人翻译, 不断更新. /* 2012-05-25 */ int sqlite3_open( const char* filename, /* 数据库文件名, 必须为 UTF-8 格式 */ sqlite3** ppDB /* 输出: SQLite 数据库句柄 */ ); 说明: 该函数打开由 filename 指定的数据库, 一个数据库连接句柄由 *ppDB 返回(

MySQL常用字符函数简介

<html> <body> <h1>MySQL常用字符函数简介</h1> <table>     <tr>         <td>CONCAT(S1,S2...Sn)</td>         <td>连接S1,S2...Sn为一个字符串</td>     </tr> </table> <p style="background-color:yel

SQL SERVER 开窗函数简介

在SQL SERVER 2005/2008支持两种排名开窗函数和聚集开窗函数. 以SQL SERVER中分面页为例,按时间顺序列出定单号. WITH OrderInfo AS ( SELECT ROW_NUMBER() OVER(ORDER BY OrderDate) AS Number, OrderID,CustomerID, EmployeeID,OrderDate FROM Orders (NOLOCK) ) SELECT Number,OrderID,CustomerID, Employ

SQL中 decode()函数简介(转载)

今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: 主要作用:将查询结果翻译成其他值(即以其他形式表现出来,以下举例说明): 使用方法: Select decode(columnname,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值) From talbename Where - 其中columnname为要选择的table中所定义的column, ·含义解释: dec

PHP中的正则表达式及PCRE函数

PCRE PHP有两种使用不同的方式来使用正则表达式:PCRE(Perl兼容表示法,preg_*)函数 和 POSIX(POSIX 扩展表示法,ereg_*) 函数.幸运的是,POSIX 家族函数从 PHP 5.3.0 开始就被弃用了. 正则表达式 界定符 经常使用的分隔符是正斜线(/).hash符号(#) 以及取反符号(~).下面的例子都是使用合法分隔符的模式 /foo bar/ #^[^0-9]$# +php+ %[a-zA-Z0-9_-]% {this is a pattern} 可以在结

SSRS 2012 聚合函数 -- 隔行换色示例

SSRS 2012 聚合函数 -- 隔行换色示例 有时我们希望报表能够显示出数据笔数的流水序号,如果要使用T-SQL查询来做出这种效果,通常必须使用RANK函数以自动产生编号,在此将介绍如何使用SSRS的RowNumber(RunningValue也可以做到)聚合函数来做出自动编号,以及将数据单元格根据序号进行隔行换色的效果. 步骤1: 使用之前的数据集,设计一张以产品大类为组的每日销售量明细表. 步骤2: 我们在交易日右方的数据单元格中撰写以下表达式(以下两者皆可).即可显示出数据序号: =R

C语言函数大全 chm含示例

C语言函数大全chm版,里面的一些代码者是语法着色的,看着很舒服,这个函数大全以a-z的字母顺序列出了所有C语言中的函数用法.代码示例,如果你对C语言编程感兴趣,那么这本函数手册绝对不可以错过的. 下载地址:点击下载 C语言函数大全 chm含示例

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

子进程创建及执行函数有三个: (1)fork();(2)exec();(3)system();    下面分别做详细介绍.(1)fork()    函数定义:    pid_t fork();    函数说明:    linux下进程在内存中由三部分数据组成:代码段.数据段.堆栈段.在一个进程中,调用fork函数,可以创建.启动一个新进程.新进程与父进程共享代码段,复制父进程的数据段和堆栈段.创建成功后,fork()会向两个进程都有返回值.向父进程的返回值为子进程的进行号,向子进程的返回值为0.

javascript回调函数(模式)原理和示例深入分析

   广大网友读懂了我之前论述的javascript原理这篇文章很容易懂 回调函数来自一种著名的编程范式--函数式编程,在基本层面上,函数式编程指定的了函数的参数.函数式编程虽然现在的使用范围变小了,但它一直被"专业的聪明的"程序员看作是一种难懂的技术,以前是这样,未来也将是如此. 幸运的是,函数式编程已经被阐述的像你我这样的一般人也能理解和使用.函数式编程最主要的技术之一就是回调函数,你很快会阅读到,实现回调函数就像传递一般的参数变量一样简单.这项技术如此的简单,以至于我都怀疑为什么