Sword pcre库使用
#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库使用的更多相关文章
- Linux下编译安装PCRE库
备注:如果没有root权限,使用 --prefix 指定安装路径 ./configure --prefix=/home/work/tools/pcre-8.xx =================== ...
- centos7下安装pcre库(pcretest)
在linux下需要对正则表达式的验证,使用的验证工具是pcretest,这个工具集成在pcre库中,下面是安装教程. 安装环境是centos7. 1)首先去官网下载压缩包文件. 其他的source网站 ...
- Linux 安装 nginx 安装PCRE库
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是 ...
- pcre库
pcre : perl compatible regular expressions , perl 兼容正则表达式 www.pcre.org 按装pcre是为了使Nginx支持具备URI重写功能的 ...
- Sword pcre库函数学习三
14.pcre_get_substring_list 原型: #include <pcre.h> int pcre_get_substring_list(const char *subje ...
- Sword pcre库函数学习二
9.pcre_free_substring_list 原型: #include <pcre.h> void pcre_free_substring_list(const char **st ...
- Sword pcre库函数学习一
0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...
- Sword 第三方库介绍二
/* uuid生成 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <ass ...
- Sword 第三方库介绍一
/* 获取字符编码 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <str ...
随机推荐
- 冰淇淋三明治 (Android 4.0)介绍
原文:http://android.eoe.cn/topic/summary 冰淇淋三明治 (Android 4.0) 是 Android 在设计上的一个里程碑.它将 Honeycomb 提供给平板的 ...
- android:ellipsize="end" 失效或者 相关的Bug
其实这文章有点傻逼. 相关的问题 TextView android:ellipsize=“end”超出一个字符时不显示…的解决 http://www.pocketdigi.com/20140122/1 ...
- Java面试题(原创)
1:int类型的范围值(可以用指数表示min--max) 答案:此题考对int占用的字节和计算机基础---2³¹-1到-2³¹ 2.java用来表示unicode的转义符是什么,并简要描述unicod ...
- 基于Django的独立运行的python脚本开发
Django框架很方便,比如其方便的ORM,如果写基于Django的独立运行脚本,主要在脚本前面加上以下代码: import sys,os,django sys.path.append(os.path ...
- sql server获取时间格式
在本文中,GetDate()获得的日期由两部分组成,分别是今天的日期和当时的时间: Select GetDate() 用DateName()就可以获得相应的年.月.日,然后再把它们连接起来就可以了: ...
- 如何在 Linux 上设置密码策略
https://linux.cn/article-2518-1.html 用户帐号管理是系统管理员最重要的工作之一.而密码安全是系统安全中最受关注的一块.在本教程中,我将为大家介绍如何在 Linux ...
- Capterra Software Categories
https://www.capterra.com/categories this software categories is valuable.
- DIOCP3-数据库DEMO
socket-Coder\DataModuleDEMO\ 本DEMO演示数据库的简单使用,其他功能需要自己扩展. 将工程的输出路径设置到socket-Coder\DataModuleDEMO\ ...
- ES6里新添加了两个很好用的东西,set和Array.from。
set是一种新的数据结构,它可以接收一个数组或者是类数组对象,自动去重其中的重复项目. 在这我们可以看见,重复的项目已经被去掉了,包括NaN.正常情况下,NaN === NaN 返回的是false,但 ...
- Django-管理站点重写admin模板
参考链接:https://blog.csdn.net/u013378306/article/details/79023242 使用Django的admin管理工具,可以快速的构建自己的管理平台,使用D ...