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());
程序实例:
- #define PCRE_STATIC // 静态库编译选项
- #include <stdio.h>
- #include <string.h>
- #include <pcre.h>
- #define OVECCOUNT 30 /* should be a multiple of 3 */
- #define EBUFLEN 128
- #define BUFLEN 1024
- int main()
- {
- pcre *re;
- const char *error;
- int erroffset;
- int ovector[OVECCOUNT];
- int rc, i;
- char src [] = "111 <title>Hello World</title> 222"; // 要被用来匹配的字符串
- char pattern [] = "<title>(.*)</(tit)le>"; // 将要被编译的字符串形式的正则表达式
- printf("String : %s/n", src);
- printf("Pattern: /"%s/"/n", pattern);
- re = pcre_compile(pattern, // pattern, 输入参数,将要被编译的字符串形式的正则表达式
- 0, // options, 输入参数,用来指定编译时的一些选项
- &error, // errptr, 输出参数,用来输出错误信息
- &erroffset, // erroffset, 输出参数,pattern中出错位置的偏移量
- NULL); // tableptr, 输入参数,用来指定字符表,一般情况用NULL
- // 返回值:被编译好的正则表达式的pcre内部表示结构
- if (re == NULL) { //如果编译失败,返回错误信息
- printf("PCRE compilation failed at offset %d: %s/n", erroffset, error);
- return 1;
- }
- rc = pcre_exec(re, // code, 输入参数,用pcre_compile编译好的正则表达结构的指针
- NULL, // extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针
- src, // subject, 输入参数,要被用来匹配的字符串
- strlen(src), // length, 输入参数, 要被用来匹配的字符串的指针
- 0, // startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量
- 0, // options, 输入参数, 用来指定匹配过程中的一些选项
- ovector, // ovector, 输出参数,用来返回匹配位置偏移量的数组
- OVECCOUNT); // ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小
- // 返回值:匹配成功返回非负数,没有匹配返回负数
- if (rc < 0) { //如果没有匹配,返回错误信息
- if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match .../n");
- else printf("Matching error %d/n", rc);
- pcre_free(re);
- return 1;
- }
- printf("/nOK, has matched .../n/n"); //没有出错,已经匹配
- for (i = 0; i < rc; i++) { //分别取出捕获分组 $0整个正则公式 $1第一个()
- char *substring_start = src + ovector[2*i];
- int substring_length = ovector[2*i+1] - ovector[2*i];
- printf("$%2d: %.*s/n", i, substring_length, substring_start);
- }
- pcre_free(re); // 编译正则表达式re 释放内存
- return 0;
- }
程序来自网上,看到有人不理解最后一个for循环的含义,ovector返回的是匹配字符串的偏移,包括起始偏移和结束偏移,所以就有循环内部的2*i处理。
PCRE正则库的使用的更多相关文章
- C正则库做DNS域名验证时的性能对比
C正则库做DNS域名验证时的性能对比 本文对C的正则库regex和pcre在做域名验证的场景下做评测. 验证DNS域名的正则表达式为: "^[0-9a-zA-Z_-]+(\\.[0-9a ...
- posix 正则库程序
使用的是posix 正则库,参考: http://see.xidian.edu.cn/cpp/html/1428.html 执行匹配的时: gcc myreg.c ip.pat 内容: ip.*[0- ...
- 使用POSIX正则库匹配一行中多个结果
正则匹配与正则表达式是什么东西我就不说了,在这里说下POSIX这个c语言正则库在对字符串进行正则匹配时取出多个结果的问题. 首先简单说明下POSIX正则库的几个函数和使用方法 第一个函数:int re ...
- 【归纳】正则表达式及Python中的正则库
正则表达式 正则表达式30分钟入门教程 runoob正则式教程 正则表达式练习题集(附答案) 元字符\b代表单词的分界处,在英文中指空格,标点符号或换行 例子:\bhi\b可以用来匹配hi这个单词,且 ...
- [原创]C语言利用pcre正则表达式库
C语言使用正则表达式,可以利用pcre库,这个比较不错的哦. 在使用过程中,利用python进行测试正则表达式是否OK,后发现出现了问题.如下所示: regex.c:11:18: warning: u ...
- 关于pcre正则表达式库libpcre
gcc 4.8中已经包含了std regex的头文件 可是没有实现,所以链接是失败的 gcc 4.9完整的支持了c++ 11的regex. 在4.9以前,可以寻求boost的regex. 不过,我更熟 ...
- [02]APUE:POSIX 正则库(#include <regex.h>)
正则匹配流程: 声明一个 regex_t 类型的变量(结构体) regcomp 函数会将“正则匹配条件”写入此结构体,并编译成特定的二进制格式(加快匹配速度) 声明一个 regmatch_t 类型的变 ...
- pcre2 正则库
\S+ 不能匹配到字符串末尾的最后一个字段
- 在C语言中利用PCRE实现正则表达式
1. PCRE简介 2. 正则表达式定义 3. PCRE正则表达式的定义 4. PCRE的函数简介 5. 使用PCRE在C语言中实现正则表达式的解析 6. PCRE函数在C语言中的使用小例子 1. P ...
随机推荐
- X86 架构和 ARM 架构
1.关于x86架构 X86是一个intel通用计算机系列的标准编号缩写,也标识一套通用的计算机指令集合,X86是由Intel推出的一种复杂指令集,用于控制芯片的运行的程序,现在X86已经广泛运用到了家 ...
- mysql下的常用操作
本文继 linux下安装mysql,记录下在工作中最常用的mysql语句 MySQL添加字段和删除字段 添加字段: alter table `user_movement_log`Add column ...
- max subquence sum(n^2)
#include<cstdio>#include<cstring>const int maxn=100005;int buf[maxn];int main(){ freopen ...
- DedeCMS中function='Html2Text(@me)的用法分析
本文实例讲述了DedeCMS中function='Html2Text(@me)的用法.分享给大家供大家参考.具体分析如下: 在使用dedecms建站网站时候,需要用到过滤html代码,调出纯文本的代码 ...
- setjmp 与 longjmp
setjmp和longjmp是C语言独有的,只有将它们结合起来使用,才能达到程序控制流有效转移的目的,按照程序员的预先设计的意图,去实现对程序中可能出现的异常进行集中处理. 先来看一下这两个函数的定义 ...
- Let's see if we could reocver Line 5.3 and above deleted chat messages or not
Forensic is a strict science and we should let the evidence speak for itself. Several months ago I s ...
- C语言开源项目
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...
- Unable to add App ID because the '10' App ID limit in '7' days has been exceeded.
Unable to add App ID because the '10' App ID limit in '7' days has been exceeded. 官方的原因是对bundle iden ...
- POJ C++程序设计 编程题#2 魔兽世界之二:装备
编程题#2: 魔兽世界之二:装备 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB ...
- URI中的常用属性
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA00AAACDCAIAAADea2ciAAAgAElEQVR4nOz9eTxU////j8/Y930pIb