#include <stdlib.h>
#include <string.h> #include "regularhelper.h"
#include "pcre/pcre.h"
#include "stringhelper.h" int regularLoop(pcre *re, char *pcSrc, size_t ovecCount, void *userData, FuncHandle callback); /********************************************************
Func Name: regularInfer
Date Created: 2018-9-29
Description: pcre识别
Input:
Output: error code
Caution:
*********************************************************/
int regularInfer(char *pcSrc, const char *pattern, void *userData, FuncHandle callback)
{
int result = ;
pcre *re = NULL;
int erroffset;
const char *pcError = NULL;
size_t ovecCount = ; if (NULL == pcSrc || NULL == pattern || NULL == userData ||NULL == callback)
{
return -;
} /*
子串的个数就是正则表达式中()的个数
*/
//计算子串的个数
ovecCount = matchOperator(pattern, '(', ')') + ; //编译正则表达式的pcre内部表示结构
re = pcre_compile(pattern, , &pcError, &erroffset, NULL);
if (NULL == re)
{
return -;
} //匹配字符串
result = regularLoop(re, pcSrc, ovecCount, userData, callback); //清理资源
pcre_free(re); return result;
} /********************************************************
Func Name: regularLoop
Date Created: 2018-9-29
Description: 循环识别字符串
Input:
Output: error code
Caution:
*********************************************************/
int regularLoop(pcre *re, char *pcSrc, size_t ovecCount, void *userData, FuncHandle callback)
{
int rc = ;
int *offsetVector = NULL;
int offset = ;
int ovecSize = ;
int match = ; //分配内存空间
//ovecSize should be a multiple of 3
ovecSize = ovecCount * ;
offsetVector = (int *)malloc(ovecSize * sizeof(int));
if (NULL == offsetVector)
{
return -;
}
memset(offsetVector, , ovecSize * sizeof(int)); while ()
{
//匹配正则表达式
////offset为偏移量,为了循环匹配
rc = pcre_exec(re, NULL, pcSrc, strlen(pcSrc), offset, , offsetVector, ovecSize);
if (rc <= )
{
break;
}
//用户自处理
callback(pcSrc, offsetVector, rc, userData);
//偏移量赋值
offset = offsetVector[];
match++;
} return match > ? : -; }
#ifdef TEST

#include "regularhelper.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void deal(char *pcData, int *regVector, size_t size, void *userArg)
{
char gcData[] = { };
int i = ; for (i = ; i < size; i++)
{
//regVector[2 * i]表示开始位置
//regVector[2 * i + 1] 表示结束位置
strncpy(gcData, pcData + regVector[ * i], regVector[ * i + ] - regVector[ * i]);
printf("key is %s\n", gcData);
}
printf("\n", gcData);
} void test()
{
char *p = NULL;
//char str[] = "http://1.203.80.138:8001/tts?user_id=speech&domain=1&language=zh&audiotype=6&rate=1&speed=5&text=asr error goodbye";
char str[] = "tabceftsfasdft12345t";
int result = ; //memset(&data, 0, sizeof(STParamList));
int data; //提取所有的参数
result = regularInfer(str, "t(.)(.)(.)(.)(.)t", &data, deal);
if (result)
{
printf("regularInfer() error .\n");
}
} int main(int argc,char * argv[])
{
test();
return ;
} #endif

Sword pcre库使用的更多相关文章

  1. Linux下编译安装PCRE库

    备注:如果没有root权限,使用 --prefix 指定安装路径 ./configure --prefix=/home/work/tools/pcre-8.xx =================== ...

  2. centos7下安装pcre库(pcretest)

    在linux下需要对正则表达式的验证,使用的验证工具是pcretest,这个工具集成在pcre库中,下面是安装教程. 安装环境是centos7. 1)首先去官网下载压缩包文件. 其他的source网站 ...

  3. Linux 安装 nginx 安装PCRE库

    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是 ...

  4. pcre库

    pcre : perl compatible  regular expressions , perl 兼容正则表达式 www.pcre.org 按装pcre是为了使Nginx支持具备URI重写功能的 ...

  5. Sword pcre库函数学习三

    14.pcre_get_substring_list 原型: #include <pcre.h> int pcre_get_substring_list(const char *subje ...

  6. Sword pcre库函数学习二

    9.pcre_free_substring_list 原型: #include <pcre.h> void pcre_free_substring_list(const char **st ...

  7. Sword pcre库函数学习一

    0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...

  8. Sword 第三方库介绍二

    /* uuid生成 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <ass ...

  9. Sword 第三方库介绍一

    /* 获取字符编码 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <str ...

随机推荐

  1. javascript读取xml文件读取节点数据的例子

    分享下用javascript读取xml文件读取节点数据方法. 读取的节点数据,还有一种情况是读取节点属性数据. <head> <title></title> < ...

  2. 如何使用xilinx pcie的源代码

    采用xilinx公司的ml555开发板,软件开发环境是ISE13.2 步骤:一,建立一个ISE工程:BMDforPCIE工程的建立方法:bmd_sx50t文件夹包含BMD Desin for the ...

  3. Linux 常见紧急情况处理方法

    使用急救盘组进行维护 急救盘组(也称为 boot/root 盘组),是系统管理员必不可少的工具.用它可以独立地启动和运行一个完整的 Linux 系统.实际上,急救盘组中的第 2 张盘上就有一个完整的 ...

  4. RVM切换ruby版本号

    RVM是Ruby Version Manager的缩写,是一个命令行工具,它能够让你轻松地安装,管理和使用多个版本号的Ruby.不同的rails项目使用等ruby和rails版本号不一样的时候.能够使 ...

  5. HTML5学习笔记(九):选择器详解

    在前面的笔记中我们已经接触过几种常见的选择器,本笔记我们将深入了解CSS的选择器. 元素选择器 最常见的 CSS 选择器是元素选择器.换句话说,文档的元素就是最基本的选择器.在 W3C 标准中,元素选 ...

  6. 使用virsh管理KVM虚拟化主机

    开机关机等操作 1. 使用virsh list 显示所有的虚拟机 [root@desktop1 ~]# virsh list --all Id Name State ----------------- ...

  7. 【Android】Sensor框架HAL层解读

    Android sensor构建 Android4.1 系统内置对传感器的支持达13种,他们分别是:加速度传感器(accelerometer).磁力传感器(magnetic field).方向传感器( ...

  8. 【Gtk】feorda下gtk安装详解

    feorda下gtk安装详解   1.yum在线安装gtk 1)pkg-config -version查看pkg-config的版本(本机测试是0.25)    2)安装必要组建:(在root权限下) ...

  9. Windows下断言的类型及实现

    一.内容综述 本文主要介绍Windows下断言assert的实现,并总结断言的不同应用准则.最后给出一个windows自定义断言的方法. 本文行文参考<Debugging Windows Pro ...

  10. Python爬取新浪微博评论数据,写入csv文件中

    因为新浪微博网页版爬虫比较困难,故采取用手机网页端爬取的方式 操作步骤如下: 1. 网页版登陆新浪微博 2.打开m.weibo.cn 3.查找自己感兴趣的话题,获取对应的数据接口链接 4.获取cook ...