pcre c语言,pcre函数详细解析

pcre c语言,pcre函数详细解析PCRE 是一个 NFA 正则引擎 不然不能提供完全与 Perl 一致的正则语法功能 但它同时也实现了 DFA 只是满足数学意义上的正则 1 pcre compile 原型 includepcre pcre compile constchar pattern intoptions constcharerr int erroffset constunsigne

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

1. pcre_compile

原型:

#include

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 *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

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

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

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

int pcre_copy_substring(const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int buffersize);

功能:根据编号获取捕获的字串。

参数:

code成功匹配的模式

subject 匹配的串

ovectorpcre_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

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

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

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

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

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

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

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

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

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

参数:

code成功匹配的模式

name   捕获名字

13. pcre_get_substring

原型:

#include

int pcre_get_substring(const char *subject, int *ovector, int stringcount, int stringnumber, const char stringptr);

功能:获取匹配的子串。

参数:

subject成功匹配的串

ovectorpcre_exec() 使用的偏移向量

stringcount    pcre_exec()的返回值

stringnumber  获取的字符串编号

stringptr      字符串指针

14. pcre_get_substring_list

原型:

#include

int pcre_get_substring_list(const char *subject, int *ovector, int stringcount, const char *listptr);

功能:获取匹配的所有子串。

参数:

subject成功匹配的串

ovectorpcre_exec() 使用的偏移向量

stringcount    pcre_exec()的返回值

listptr      字符串列表的指针

15. pcre_info

原型:

#include

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

已过时,使用pcre_fullinfo替代。

16. pcre_maketables

原型:

#include

const unsigned char *pcre_maketables(void);

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

参数:

示例:

Line2759 tables = pcre_maketables();

17. pcre_refcount

原型:

#include

int pcre_refcount(pcre *code, int adjust);

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

参数:

code已编译的模式

adjust      调整的引用计数值

18. pcre_study

原型:

#include

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

char *pcre_version(void);

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

参数:

示例:

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215339.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午2:08
下一篇 2026年3月18日 下午2:09


相关推荐

  • pycharm连接mysql数据库代码_怎么把Python与pycharm连接

    pycharm连接mysql数据库代码_怎么把Python与pycharm连接PyCharm版本:2020.3使用PyCharm连接数据库(MySQL)前言步骤SQLite总结前言最好使用PyCharmProfessional版步骤前期需要安装包(比如:pymysql)1.在PyCharm右侧工具栏有Database,点击打开如果没有,则在view|ToolWindows|Database选择显示2.点击Database中的+,选择DataSource,选择MySQL3.填写远程连接MySQL数据库的参数Host:

    2022年8月28日
    6
  • 《javascript高级编程》读书笔记(两)javascript基本概念

    《javascript高级编程》读书笔记(两)javascript基本概念

    2022年1月11日
    39
  • java物联网框架_物联网学java吗

    java物联网框架_物联网学java吗基于java的物联网架构实现前言:19年11月开始从【金融】行业转【物联网】,路途坎坷,一个人摸索前进,不过也学到了很多新的东西,交了很多好朋友,在此感谢各位!以下是一些经验分享,希望能帮到有需要的朋友。1、架构思路考虑了很久打算用springboot+mysql去实现,因为熟悉这个框架,而且能减轻70%的机械性开发工作量,以后改springcloud也方便(注意逻辑实现不然工作量很大)。物联网和互联网可以说是有共同点的,但是也有很多的不一样。先说协议,互联网很多都是https或者ht

    2026年1月18日
    5
  • nginx和apache优缺点

    nginx和apache优缺点nginx与Apache的对比今天准备较详细的对比一下apachehttpd与nginx两个web服务器的异同点、优缺点。由于我并不是做web开发的,所以有什么理解错误还请指出,想要了解它们是因为工作中有时候会用到它,有系统中用到了nginx+apache。本文绝大多数资料都是摘抄网上,自己做的只就是整合网上零散的资源然后加上自己的一点见解。简单的说apachehttpd和nginx都是we…

    2022年5月16日
    42
  • 完整软件研发流程「建议收藏」

    完整软件研发流程「建议收藏」软件产品开发流程:下图所示的是一个软件产品开发大体上所需要经历的全部流程:1、启动在项目启动阶段,主要确定项目的目标及其可行性。我们需要对项目的背景、干系人、解决的问题等等进行了解。并编制项目章程和组建项目团队,包括:产品经理、架构工程师、UI工程师、开发工程师、测试工程师等。完成以上准备工作之后,召开项目启动会,启动会结束则进入下一步的工作。2、规划…

    2022年6月16日
    33
  • 码流 / 码率 / 比特率 / 帧速率 / 分辨率的区别[通俗易懂]

    码流 / 码率 / 比特率 / 帧速率 / 分辨率的区别[通俗易懂]码流/码率/比特率/帧速率/分辨率/高清的区别2015年03月13日10:40:30阅读数:143980GOP/ 码流/码率/比特率/帧速率/分辨率 GOP(Groupofpicture)      关键帧的周期,也就是两个IDR帧之间的距离,一个帧组的最大帧数,一般而言,每一秒视频至少需要使用1个关键帧。增加关键帧个数可…

    2022年5月28日
    73

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号