PCRE正则库的使用

摘要:
PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能。PCRE提供了19个接口函数,为了简单介绍,使用PCRE内带的测试程序示例用法。参数:pattern正则表达式options为0,或者其他参数选项errorcodeptr存放出错码errptr出错消息erroffset出错位置tableptr指向一个字符数组的指针,可以设置为空NULL3.pcre_config原型:#includeintpcre_config;功能:查询当前PCRE版本中使用的选项信息。参数:what选项名where存储结果的位置示例:Line1312pcre_config;4.pcre_copy_named_substring原型:#includeintpcre_copy_named_substring;功能:根据名字获取捕获的字串。
使用pcre编写C或C++程序,然后编译。
对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcre file.c
对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcrecpp file.cpp

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

示例:

L1720re = 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()使用的偏移向量

stringcountpcre_exec()的返回值

stringname捕获字串的名字

buffer用来存储的缓冲区

buffersize缓冲区大小

示例:

Line2730int 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()使用的偏移向量

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

示例:

Line2730count = 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指向字符串的指针

示例:

Line2730const 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指向字符串数组的指针

示例:

Line2773const 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编译好的模式

extrapcre_study()的返回值,或者NULL

what什么信息

where存储位置

示例:

Line997if ((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()使用的偏移向量

stringcountpcre_exec()的返回值

stringname捕获字串的名字

stringptr存放结果的字符串指针

示例:

Line2759const 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()使用的偏移向量

stringcountpcre_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()使用的偏移向量

stringcountpcre_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()替换掉内建的字符表。

参数:

示例:

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

示例:

Line1797extra = pcre_study(re, study_options, &error);

19.pcre_version

原型:

#include <pcre.h>

char *pcre_version(void);

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

参数:

示例:

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

程序实例:

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

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

免责声明:文章转载自《PCRE正则库的使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇CentOS系统安全配置jmeter之Bean Shell Sampler使用四则运算下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WebSocket 详解

HTTP 协议在设计上就是一个单向的网络协议,服务器只能被动的接收请求,然后返回相应的数据。对于需要双向通信的场景,虽然可以通过轮询,Comet 等方式实现,但每次链接都要三次握手,效率低下。 与http比较: 1.都基于 TCP 的、应用层的可靠性传输协议 2.WebSocket 在握手时的数据是通过 HTTP 传输的,一旦连接建立后就不再依赖 HTT...

TCP/IP网络编程系列之三(初级)

TCP/IP网络编程系列之三-地址族与数据序列 分配给套接字的IP地址和端口 IP是Internet Protocol (网络协议)的简写,是为首发网络数据而分配给计算机的值。端口号并非赋予计算机值,而是为了区分程序中创建的套接字而分配给套接字的序号。 网络地址 网络地址分为IPV4和IPV6,分别你别为4个字节地址簇和6个字节地址簇。IPV4标准的4个字...

转:程序内存空间(代码段、数据段、堆栈段)

https://blog.csdn.net/ywcpig/article/details/52303745 在冯诺依曼的体系结构中,一个进程必须有:代码段,堆栈段,数据段。 进程的虚拟地址空间图示如下: BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域。BSS是英文Block Started by Sym...

Qt 访问网络

一、前言 Qt 中访问网络使用 QNetworkAccessManager,它的 API 是异步的,这样在访问网络的时候不需要启动一个线程,在线程里执行请求的代码。(但这一点在有时候需要阻塞时就是个麻烦了) 需要注意一点的是,请求响应的对象 QNetworkReply 需要我们自己手动的删除,一般都会在 QNetworkAccessManager::fin...

C++中的头文件和源文件

一、C++编译模式通常,在一个C++程序中,只包含两类文件——.cpp文件和.h文件。其中,.cpp文件被称作C++源文件,里面放的都是C++的源代码;而.h文件则被称作C++头文件,里面放的也是C++的源代码。C+ +语言支持“分别编译”(separate compilation)。也就是说,一个程序所有的内容,可以分成不同的部分分别放在不同的.cpp文...

链表面试题

1、逆置链表 假设链表现在是 4->3->2->1->NULL逆置后的链表是 1->2->3->4->NULL步骤:第一步:先把4用临时指针tmp保存起来,cur指向下一个节点,即cur指向3 第二步:令tmp指向newNode,4是第一个节点,则4的next为NULL,即令tmp->next =ne...