PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能。但它同时也实现了DFA,只是满足数学意义上的正则。
PCRE提供了19个接口函数,为了简单介绍,使用PCRE内带的测试程序(pcretest.c)示例用法。
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_THRESHO LD, &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 匹配的串 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 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 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 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)) 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 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 成功匹配的串 ovector pcre_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 成功匹配的串 ovector pcre_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()替换掉内建的字符表。
参数:
示例:
Line2759tables = 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 出错消息 示例:
Line1797extra = pcre_study(re, study_options, &error);
19. pcre_version 原型: #include char *pcre_version(void); 功能:返回PCRE的版本信息。
参数:
示例:
Line1384if (!quiet) fprintf(outfile, "PCRE version %s\n\n", pcre_version());
程序实例: #define PCRE_STATIC // 静态库编译选项 #include #include #include #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 Hello World 222"; // 要被用来匹配的字符串 char pattern [] = "(.*)"; // 将要被编译的字符串形式的正则表达式 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 正则公式 $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处理。
发表评论
-
Shell 字符串截取方法
2012-07-06 09:52 894Shell下截取字符串的功能非常少,不过有时在判断字符串前 ... -
组件服务中找不到office word
2012-07-03 13:44 1263在.NET环境下,使用word组件进行word操作,实现w ... -
AS3.0相关知识
2012-07-02 13:16 647as3.0 2010-01-06 0 ... -
JAVA+fms
2012-07-02 13:16 814最近完成的 Java Flex Fms 视频聊天程序,虽然 ... -
BulkLoader多个文件一起加载
2012-07-02 13:16 569flex的单线程以及事件机制,导致了它在加载文件的时候不能 ... -
学习组件拖拽
2012-07-02 13:16 466/本篇文章来源于ACCP教程网, ... -
Cairngorm示例:用户登录
2012-07-02 13:16 3771. Cairngorm简介 Cairngorm是 ... -
[ActionScript3.0][My flex]读书笔记三之 模块化编程
2012-07-01 10:45 6051.加载mxml模块 第一步: 新建mxml模块:Data ... -
Flex+C# WebService连接的例子
2012-07-01 10:45 625Webservie后台的代码: using System; ... -
[ActionScript3.0][My flex]读书笔记四之 图标模块应用实例
2012-07-01 10:45 6461.chartdata.xml 2.MyCo ... -
FLEX数据绑定要点(转)
2012-07-01 10:45 5861.什么是数据绑定 ... -
Removing items from a Flex DataGrid control using the DragManager class
2012-07-01 10:45 600You want to remove items from ... -
flex 打印 预览
2012-06-30 17:09 630flex 打印 预览 2011年03 ... -
flex学习-------flex与as基础
2012-06-30 17:09 531flex学习-------flex与as基础 2010年10 ... -
关于FLex Unit 的 Runtime Exception
2012-06-30 17:08 524关于FLex Unit 的 Runtime Exception ... -
关于Flex4工程和 Alternativa3D 7.5.1的疑问
2012-06-30 17:08 630关于Flex4工程和 Alternativa3D 7.5.1的 ... -
The architecture of Flex and Java applications two (Flex 和 Java 应用程序架构 2)
2012-06-30 17:08 434The architecture of Flex and Ja ...
相关推荐
主要介绍了PHP正则表达式处理函数(PCRE 函数),结合实例形式总结分析了php正则表达式preg_replace、preg_match、preg_match_all、preg_split及preg_quote等函数相关使用技巧,需要的朋友可以参考下
PCRE提供了19个接口函数,为了简单介绍,使用PCRE内带的测试程序(pcretest.c)示例用法
pcre Perl兼容JavaScript正则表达式安装npm install @desertnet/pcre用法在内部,此模块使用库,该库在WebAssembly实例中运行。 这有一个副作用,要求您在使用此模块时做一些不寻常的事情:初始化在调用任何构造函数...
它不使用 pcre 函数或 ereg 函数,并允许语法代码逐行不断地减少内存使用。 您可以编写自己喜欢的代码语法,但请记住不要使用长字符串变量来防止大量内存使用或崩溃。 如果您愿意,请将带有示例的关键字和字符串...
PHP核心preg_*函数没有提供在使用前验证正则表达式的任何好方法。 一些核心函数针对无效的正则表达式返回false,但它们还会发出无法捕获的警告。 RegexGuard是一个包装程序,可让您验证正则表达式并使API远离无法...
如下所示: $preg= '/xue[\s\S]*?... 您可能感兴趣的文章:php 正则去掉 </p> 空格 与<p><br></p>php正则表达式使用方法整理集合PHP常用正则表达式精选(推荐)PHP正则表达式处理函数(PCRE 函数)实例小结
6.4PCRE兼容正则表达式函数 PHP数组(教学录像:56分29秒) 7.1了解数组 7.2声明数组 7.3数组的类型 7.4输出数组 7.5数组的构造 7.6遍历数组 7.7合并数组 7.8字符串与数组的转换 7.9统计数组元素个数 7.10数组排序 ...
6.4PCRE兼容正则表达式函数 PHP数组(教学录像:56分29秒) 7.1了解数组 7.2声明数组 7.3数组的类型 7.4输出数组 7.5数组的构造 7.6遍历数组 7.7合并数组 7.8字符串与数组的转换 7.9统计数组元素个数 7.10数组排序 ...
1、下载PCRE, 是一个用C语言编写的正则表达式函数库 [root@localhost pcre-8.36]# cd /tmp/download/ [root@localhost download]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz ...
本节会向大家介绍模式修正符的概念、模式修正符的构成,以及结合实例的模式修正符的演示,大家在学习完本节内容之后,就完全可以读懂正则表达式了。 什么是模式修正符? 1, 模式修正符就是几个字母,我们在每个正则...
许多类中都有大量的链接函数,因此您可以轻松获取额外的数据,但是请记住,如果您不使用缓存,则每次都会加载xml 别忘了使用utf-8编码 测试愉快! 要求: PHP 5.3以上 扩充功能: 卷曲 多姆 的libxml pcre ...
本文实例讲述了php正则修正符用法。分享给大家供大家参考,具体如下: <?... //标记在整个模式之外; // 例://$mode="/\bis\b/U",其中U在外面; //修正符:i 不区分大小写的匹配;... //$str和$mode仍可以匹