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 ...
随机推荐
- SQL SERVER 数据库被标记为“可疑”的解决办法
问题背景: 日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无故停 ...
- JSP中四种传递参数中文乱码问题
查看来源:http://blog.csdn.net/hackerain/article/details/6776083
- ElasticSearch + xpack 使用
ElasticSearch 是一个高可用开源全文检索和分析组件.提供存储服务,搜索服务,大数据准实时分析等.一般用于提供一些提供复杂搜索的应.我们为什么要选择 ElasticSearch ?因为它是一 ...
- Merge into 详细介绍
/*Merge into 详细介绍 MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句. 通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询, 连接条 ...
- nodejs中aes-128-cbc加密和解密
和java程序进行交互的时候,java那边使用AES 128位填充模式:AES/CBC/PKCS5Padding加密方法,在nodejs中采用对应的aes-128-cbc加密方法就能对应上,因为有使用 ...
- SECURITY ONION:防御领域的kali
https://securityonion.net/ Security Onion is a free and open source Linux distribution for intrusion ...
- Spark(Hive) SQL中UDF的使用(Python)【转】
相对于使用MapReduce或者Spark Application的方式进行数据分析,使用Hive SQL或Spark SQL能为我们省去不少的代码工作量,而Hive SQL或Spark SQL本身内 ...
- linux内核编译错误集锦
1,我是用的ubuntu16.04 32位系统编译内核linux-3.0.1出现如下错误:Can't use 'defined(@array)' (Maybe you should just omit ...
- pom.xml里发布和下载包
1.下载包 在<project>标签中 <repositories> <repository> <id>nexus</id> <url ...
- table 中 文字长度大于td宽度,导致文字换行 解决方案
1.TD不换行 nowrap属性 表格table的td单元格中,文字长了往往会撑开单元格,但是如果table都不够宽了,就换行了好像(不要较真其他情况,我只说会换行的情况).换行后的表格显得乱糟糟,不 ...