PCRE函数简介和使用示例

2024-02-12 20:32
文章标签 函数 使用 示例 简介 pcre

本文主要是介绍PCRE函数简介和使用示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

 

程序实例:

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

这篇关于PCRE函数简介和使用示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/703529

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

git使用的说明总结

Git使用说明 下载安装(下载地址) macOS: Git - Downloading macOS Windows: Git - Downloading Windows Linux/Unix: Git (git-scm.com) 创建新仓库 本地创建新仓库:创建新文件夹,进入文件夹目录,执行指令 git init ,用以创建新的git 克隆仓库 执行指令用以创建一个本地仓库的

ASIO网络调试助手之一:简介

多年前,写过几篇《Boost.Asio C++网络编程》的学习文章,一直没机会实践。最近项目中用到了Asio,于是抽空写了个网络调试助手。 开发环境: Win10 Qt5.12.6 + Asio(standalone) + spdlog 支持协议: UDP + TCP Client + TCP Server 独立的Asio(http://www.think-async.com)只包含了头文件,不依